How to create a dynamic variable in batch.

225 pts.
Tags:
Automation
Batch
Scripting
Hello all, I am attempting to write a batch file that will auto-move a file from a specific directory once it is created(e.g. if exists move file.png to backupfile.png) My issue is to prevent the file from being over-written, as I need all iterations of the file saved into separate files, I would like to append either the system time or a number say 1,2,3 etc so that I have unique file names everytime. I tried using the system time but cannot find a way to set this as a variable that I can append to the file name when it is moved. Does anyone know how to do this? Thank you, Techy1984

Answer Wiki

Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Discuss This Question: 9  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • carlosdl
    What is the operating system or scripting language that you want to use ? Can you post your current code ?
    69,365 pointsBadges:
    report
  • jutpro
    I wrote and tested the below DOS batch script. If the script you want is in DOS then this might help. The output file looked like this, 20121012112347_test.txt  @echo off set DATETIME=%date:~-4,4%%date:~-10,2%%date:~-7,2%%HOUR%%time:~0,2%%time:~3,2%%time:~6,2% echo test datetime in file name >> %DATETIME%_test.txt
    480 pointsBadges:
    report
  • Techy1984
    The language is DOS on Windows XP through 8 I do tech support with rendering software and so I need to be able to capture each iteration of an image. So my though was to move the file to a new directory with a new name so as creating a visual log of the renders progress.My current code looks like this:@echo off :A for %%x in (%time%) do set %%x=%time% move image.png "%UserProfile%\Desktop\Frames\%%x-image.png" goto A But all I get is a file called '-image.png'I will try jutpro's suggestion but that looks like it should work.Thank you very much!
    225 pointsBadges:
    report
  • TomLiotta
    for %%x in (%time%) do set %%x=%time%   That command should start by building a list of files that it finds that have the name [%time%]. For every file with that name, it sets variable %%x to hold the value [%time%]. Then it uses the variable %%x to do the SET that sets the value of %%x to equal %time%. (Yes, the way it's coded, it will try to do it twice -- once for the IN() and again for the SET.)   Now, if there are no files named [%time%], it skips the SET and goes on to the MOVE command. Of course, in that case, %%x never gets a value assigned to it.   That's how I read the FOR command. Does that make sense?   Tom
    125,585 pointsBadges:
    report
  • carlosdl
    "That command should start by building a list of files..." As far as I remember, a DOS' FOR loop does not necessarily involve files.  It can work on lists of many kinds of elements, and in this case I don't think the intention is to loop through a list of files, but I don't understand the real intention either.  Techy1984, can you explain your current code ? Anyway, the %time% variable holds the system time in 'hh:mi:ss.ms', which is includes characters not allowed in file names, so the format needs to be modified before renaming the file (something that jutpro's code takes into account). 
    69,365 pointsBadges:
    report
  • Techy1984
    jutpro's solution worked flawlessly! Thank you very much. I was able to create the following batch file: @echo off :A set DATETIME=%date:~-4,4%%date:~-10,2%%date:~-7,2%%HOUR%%time:~0,2%%time:~3,2%%time:~6,2% move render.png "%UserProfile%\Desktop\Frames\%DATETIME%_render.png" goto A
    225 pointsBadges:
    report
  • Techy1984
    carlosdl: The intent was simply as mentioned in my second post. I am rendering a scene in a rendering program. The program has the ability to automatically save the latest pass/iteration of the scene to an image file.However, it can only overwrite the same file not create a new file with every pass. I wanted to show a progressive set of images/frames for the render so my thought was to move the file and rename it to a unique and sortable file name so that each new pass is saved into it's own file. jutpro's code allowed me to do this using the batch loop I posted.Thank you all for your input this is why I like this site when I am stuck with no google-able answers =)
    225 pointsBadges:
    report
  • carlosdl
    Great.  Thanks for the feedback.
    69,365 pointsBadges:
    report
  • jutpro
    Good job. I am glad I was able to help.
    480 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following