Computer Hope
Microsoft => Microsoft DOS => Topic started by: BRIANH on September 29, 2008, 03:00:11 PM
-
I have reviewed several previous posted Q's and haven't seen anything which seems to fit my issue.
I have files named -- FILE_1.TXT, FILE_2.TXT FILE_3.TXT
All files are in a single directory....
I'd like to rename all files as FILE_1-current date with no '/'s.TXT
FILE_2-current date with no '/'s.TXT
FILE_3-current date with no '/'s.TXT
I've tried ren D:\TEST\*.TXT *-%date:/=%.txt
I end up with -- FILE_1.txt-20080929.txt
It repeats the .txt extention which is not what I want...
Hopefully someone will have a suggestion...
thanks
-
well first you have to know that two files cant have the same filename. but i have a solution for you right here. place the batch program in the same directory as the files and run it, as you can see i've added a number after the am or pm(depending upon the time) that separates the files so you wont have the same filename error. save the attachment as a BAT file.
hope this works
,Nick
[Saving space - attachment deleted by admin]
-
thanks for your reply.. however I'm not entirely clear re: your solution.. I don't see it...
-
Renaming files using wildcards can lead to some very bizarre filenames. Might be easier to process one file at a time:
for %%V in (*.txt) do ren "%%V" "%%~nV_%date:/=%.txt"
8)
-
tested this on my laptop then ran it on our server:
for %%V in (*.txt) do ren "%%V" "%%~nV-%date:/=%.txt"
on my laptop I ended up with what I was targeting:
anomlog.1a28-09302008.txt
on the server I ended up with something different due no doubt to how the DATE is formatted..
imagine my surprise.....
anomlog.1a28-Tue 09302008.txt
Then attempted to remove the 'TUE ' from the filename and have not been successful..
ran this test bat file
CD D:\TEST
REM @echo off
REM setlocal enabledelayedexpansion
for /f %%F in (*.TXT) do (
set oldname=%%F
set newname=!oldname!
set newname=!newname:'TUE '=!
ren !oldname! !newname!
)
I ended up with this:
D:\>CD D:\TEST
D:\TEST>REM @echo off
D:\TEST>REM setlocal enabledelayedexpan
D:\TEST>for /F %F in (*.TXT) do (
set oldname=%F
set newname=!oldname:'TUE '=!
ren !oldname! !newname!
)
The system cannot find the file *.TXT.
D:\TEST>pause
Press any key to continue . . .
So, as always... anyone with a suggestion or two.. if you'll take the time to pass them along to me.. I'll appreciate your time and effort...
-
Please answer a couple of questions:
you have filenames in the format:
filename.extension
and you want them to be renamed to
filename-date.extension
where date is todays date and extension is always .txt
is that right?
It sounds like your system date format settings are USA style, that is today is shown as "Tue 09/30/2008"
Is that right?
-
thanks for your reply.. however I'm not entirely clear re: your solution.. I don't see it...
its the timestamp txt file i attached. save it as a BAT file
-
where date is todays date and extension is always .txt
is that right? >>>>>>>>>>>>>>> correct
It sounds like your system date format settings are USA style, that is today is shown as "Tue 09/30/2008"
Is that right? >>>>>>>>>>>>>>> according to what I see, correct.
-
OK here's a quick hack I cooked up
@echo off
setlocal enabledelayedexpansion
REM get MM, DD and YYYY independent of system date format settings
echo Wscript.echo ^(DatePart^("YYYY", Date^)^)>DateYear.vbs
echo Wscript.echo ^(DatePart^("M", Date^)^)>DateMonth.vbs
echo Wscript.echo ^(DatePart^("D", Date^)^)>DateDay.vbs
for /f %%A in ('DateYear.vbs //nologo') do set /a yyyy=%%A
for /f %%A in ('DateMonth.vbs //nologo') do set /a mm=%%A
for /f %%A in ('DateDay.vbs //nologo') do set /a dd=%%A
del DateYear.vbs
del DateMonth.vbs
del DateDay.vbs
if %mm% LSS 10 set mm=0%mm%
if %dd% LSS 10 set dd=0%dd%
set DateString=%mm%%dd%%yyyy%
echo %DateString%
REM remove ECHO when happy
for /f "delims==" %%A in ('dir /b *.txt') do (
set oldname=%%A
set newname=%%~nA-%DateString%.txt
ECHO ren "!oldname!" "!newname!"
)
-
I just realised, if you leave the renamed files where they are, the next time you run the batch (e.g. the enxt or a later day) then they will get caught and processed because they still have a .txt extension, so why not make a sub folder named for the date and move them there?
@echo off
setlocal enabledelayedexpansion
REM get MM, DD and YYYY independent of system date format settings
echo Wscript.echo ^(DatePart^("YYYY", Date^)^)>DateYear.vbs
echo Wscript.echo ^(DatePart^("M", Date^)^)>DateMonth.vbs
echo Wscript.echo ^(DatePart^("D", Date^)^)>DateDay.vbs
for /f %%A in ('DateYear.vbs //nologo') do set /a yyyy=%%A
for /f %%A in ('DateMonth.vbs //nologo') do set /a mm=%%A
for /f %%A in ('DateDay.vbs //nologo') do set /a dd=%%A
del DateYear.vbs
del DateMonth.vbs
del DateDay.vbs
REM add leading zero to single digit numbers
if %mm% LSS 10 set mm=0%mm%
if %dd% LSS 10 set dd=0%dd%
REM create date string in desired format
set DateString=%mm%%dd%%yyyy%
REM use date string for folder name
set DirName=%DateString%
MEM In various lines below,
REM Remove ECHO *** when you are happy
REM create folder if it does not yet exist
if not exist %DirName% (
echo Creating folder %DirName%
ECHO *** MD %DirName%
)
REM if there are any text files in this folder
if exist *.txt (
Echo Found text files to process
REM get their names and process in this loop which follows
for /f "delims==" %%A in ('dir /b *.txt') do (
REM store filename
set oldname=%%A
REM create new name
set newname=%%~nA-%DateString%.txt
REM rename file
echo Renaming !oldname! to !newname!
ECHO *** ren "!oldname!" "!newname!"
REM if file does not exist in folder move it there
if not exist "%DirName%\!newname!" (
echo Moving !newname! to %DirName%
ECHO *** move "!newname!" %DirName%
)
)
)
-
Macdad, Sidewinder, Dias de verano......
To all... thanks for your help and your time... I have something now which does
what I want.. files have the current date to their name and the .txt extension is
maintained....... And, during the process I've learned somethings..still a raw beginner but somewhat more familiar with somethings..
Strictly as a sidebar observation, I noticed that when I open a DOS window and run the bat file, I get a running status of what the move command is doing. When
I attempted to force a non-display, I used - move infile outfile > &. This worked however there appears to be something else going on as well. I have a pause command following the move command and it doesn't seem to be executed...
I could direct the move command output to a file but this would be a file which would have little importance and I'd probably want to delete it.
I've tried a couple of things but not knowing everything that the > & is setting up, I've not had any success...
Not super important but I found it curious...
-
redirect to the NUL device- as in
Move infile outfile > NUL
I recall being called out on this one because of filesystem differences... I'm pretty sure that was Dias at some point. Feel free to call me out again!
-
BC_Programmer...
thanks.. managed to flash read several posts and found the > nul
thanks again for your help...
-
The only NUL thing to do with filesystem differences that I can recall is using the existence of foldername\NUL as a test for the existence of foldername, which does not always work reliably in NTFS.
-
The only NUL thing to do with filesystem differences that I can recall is using the existence of foldername\NUL as a test for the existence of foldername, which does not always work reliably in NTFS.
All I remember is I got called out for something to do with NUL- I recall now it was some fellow trying to determine the existence of a folder.
It's good to know I'm not always wrong.
-
A bit of a differnet twist to my filename rename issue in a batch file.
I basically was attempting to append a - and a numeric value beginning with 1 to
each file in a directory. My aim was to increase the variable containing a numeric value for each file found and append it to the filename.. e.g. filex-1, filey-2, filez-3.
I tried the following but ended up with the same value of 0 in the variable. Not sure why the variable was not being adjusted..
Hopefully someone can spot where I've gone wrong in the code...
mycode
setlocal enabledelayedexpansion
set filenum=0
cd D:\TEST
for %%A in (*.txt) do (
@echo on
set /A filenum=%filenum%+1
set oldname=%%A
set newname=%%~nA-%filenum%.txt
rem echo "oldname"
rem echo "newname"
ren "!oldname!" "!newname!"
rem pause
)
-
Not sure why the variable was not being adjusted..
Because %filenum% should have been !filenum!
-
I suspect we're getting close..however the result is not what I had targeted...
The updated file names all have the same variable value..
e.g. filex-1, file-y-1, filez-1
I'm trying to get ---- filex-1, filey-2, filez-3.
My fault in not being able to articulate clearly what I wanted...
The for loop now is coded as:
for %%A in (*.txt) do (
@echo on
set /A filenum=%filenum%+1
set oldname=%%A
set newname=%%~nA-!filenum!.txt
rem echo "oldname"
rem echo "newname"
ren "!oldname!" "!newname!"
rem pause
)
-
You only changed the second %filenum% when you should have changed both.
-
Terrific !!!! I have exactly what I want...
thanks fo much !!!
One of the issues I have with some of the code is that I don't fully understand how some of the sublties really work... e.g. the significance of the !variable! versus
%variable% within the scope of the FOR statement.. I assume !variable! indicates that the new value of the variable is to be used each time through the loop as opposed to the %variable% which appears to use only the initial value of the variable set during the first loop ... again, just guessing... I've attempted to find some information on this and haven't had much luck...the help of the FOR statement does not explain this well....
-
The topic you need to learn about is "delayed expansion". There is a ton of stuff you can find out by Googling, explained much better than I can, However here is a brief run through:
When a batch file containing only % sign variables is run, the first thing that happens is that ALL of the variables are "expanded" into their known values. Then the code is executed. Any variables which are SET before a parenthetical expression such as a FOR loop or an extended IF statement, keep the value they had before, and those which are SET inside the expression are blank, as you have noticed.
However, with Windows 2000 came delayed expansion. You enable it with setlocal enabledelayedexpansion and you use variables with ! instead of %.
-
Thanks for the tip... I'll follow up....and do some reading...