Jump to content

Save command output as variable?


Recommended Posts

Hi,

I'm writing a simple batch script. I'm having trouble creating a correct variable however...

I have a file named "_DateFile" that contains a date like this: 2005-05-05 . I wish to parse that file to get the date in it, and name a file in the script using that date.

This is as far as I have gotten:

Try to create variable from the command (grep will parse "_DateFile" for any line containing 2005 and output it):

set olddate=´grep 2005 _DateFile´

If I now type %olddate%, I do get the date that is stored in _DateFile. Success, I thought - but no. If I now want to create a file USING that old date, it will not work.

For example:

echo 1 > %olddate%.mydocument.doc

will not create the file 2005-05-05.mydocument.doc. It creates a file called "grep".

Hm.. It seems that Windows does not in fact store the *output* of the command in olddate, but the command itself.. If I do

echo %olddate%, I get:

grep 2005 _DateFile

Does anybody know how to make the Windows variable remember the *value* outputted from a command - and not the command itself?

Thanks alot!

Link to comment
Share on other sites

I dont know if this will help you buy why not just put the code that generates the _DateFile value in the first place into your batch script? So it looks something like this:

#Generate Date Code Here

#Continue your batch code Here

echo 1 > %olddate%.mydocument.doc

Nah, I can't do that because I won't know what date to put into the _DateFile.. The value in the _DateFile will be created sometime (we don't know when) before I run the script I write now.

I've got it all figure out, except for this last piece...

I did some research, and found this page, but it didn't contain any information about doing what I want.. Maybe SET isn't the command I need at all?

Anybody know anything about these kinds of stuff?

Link to comment
Share on other sites

Thanks Scarecrow Man, that did the trick. For all future seekers, here are my final (but sketchy results):

Script 1, upload files to server:

This packs folder 1 and folder 2 into a rar, uploads the rar and the datefile unto the server.

REM Create archive(s) of Folder 1 and Folder 2, but make each rar-file maximum 50MB large.

rar a -ep1 -r -v50176 %date%.MyData.rar "PathToFolder1" "PathToFolder2"

ECHO ENCRYPT the file...

gpg --output %date%.MyData.rar.gpg --recipient MyKey --encrypt %date%.MyData.rar

ECHO DELETE the unencrypted file...

del %date%.MyData.rar

ECHO UPLOAD the file... (enter password)

echo %date% > _DateFile

curl --user [email protected] --ftp-ssl-control --insecure --ftp-ssl-reqd --sslv3 --upload-file {%date%.MyData.rar.gpg,_DateFile} PathToUploadFilesOnServer/

ECHO DELETE the local encrypted file...

del %date%.MyData.rar.gpg

del _DateFile

END

Script 2, download latest snapshot of files from the server:

This downloads the latest uploaded RAR archive from the server.

@ECHO OFF

REM Download DateFile that has only the latest date inside of it:

curl --user [email protected] --ftp-ssl-control --insecure --ftp-ssl-reqd --sslv3 --output _DateFile PathTo/_DateFile

REM Get the date out of the file and make it a variable:

awk "/2008/ {print \"set latestdate=\" $1}" _DateFile >tmp_.bat

call tmp_.bat

echo The latest date of the file on server is (according to datefile): %latestdate%

echo Continue with download? If not press CTRL-C. Otherwise ENTER.

pause

REM Use the variable to get the latest snapshot:

curl --user [email protected] --ftp-ssl-control --insecure --ftp-ssl-reqd --sslv3 --output %latestdate%.MyData.rar.gpg PathTo/%latestdate%.MyData.rar.gpg

REM Decrypt, unpack, etc:

gpg --output %latestdate%.MyData.rar --decrypt %latestdate%.MyData.rar.gpg

REM Clean up encrypted file.

del %latestdate%.MyData.rar.gpg

REM Unpack.

rar x %latestdate%.MyData.rar _Downloaded\MyData\

REM Clean up archive.

del %latestdate%.MyData.rar

END

Maybe I'll merge them later into one script. I run the scripts from the same folder as I have the executables (gawk, gpg-to-go, rar and curl - all of which are downloadable from the Internet - well, except for rar which you have to buy if you want to be able to create split archives).

So I have one folder with everything related to the scripts.

Hm, as a side note, the gpg-to-go isn't really important if you don't care about the privacy of the files uploaded.. However, if you script it like this you will never notice it is encrypted on the server at all (I have no password for my secret key -> no need for entering passwords), and the you will be safe from the prying eyes of the Big Brother Internet Overlords.

A second side note: Even though I have the line for splitting the rar archives into 50MB pieces, the files I'm uploading are always smaller. The script will not work for more than one file (I'll have to amend it later for that to work... With loops and internal counters somehow, (curl can't take *.rar ..) well that's a later problem)

Thanks for the help guys, problem solved.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Privacy Policy