Solved Renaming a file with current date & time.

August 5, 2012 at 19:17:26
Specs: Windows 7
Hi any body there
i am trying to move csv files from one folder to other. while moving i want to rename the files. i am using below code.


@echo off & setLocal EnableDelayedExpansion

:LOOP
set /A limit+=1
TIMEOUT 1

FOR %%i IN (C:\Users\sneha\Desktop\julyFolder\*.xlsx) DO (
set /A N+=1
@Echo Off
@For /F "tokens=1,2,3,4 delims=/ " %%A in ('Date /t') do @(
Set Day=%%B
Set Month=%%C
Set Year=%%D
Set All=%%D%%C%%B
)
@For /F "tokens=1,2,3 delims=:,. " %%A in ('echo %time%') do @(
Set Hour=%%A
Set Min=%%B
Set Sec=%%C
Set Allm=%%A%%B%%C
)


move "%%i" C:\Users\sneha\Desktop\desti\sneha_%All%%Allm%.xlsx
if !N! geq %limit% GOTO :LOOP
)

::DONE


it is working fine but it is not appending the 1st file name. For example if i am having 5 files in source n i run the above batch file thn leaving the first file i m getting all other 4 files in destination with current date n time. what should i do so that i can have the first file with current date and time.


See More: Renaming a file with current date & time.

Report •

✔ Best Answer
August 6, 2012 at 12:07:42
well, no-where. You don't need it. (besides, n and limit are always equal anyway). Try this re-built version:

@echo off & setLocal EnableDelayedExpansion

FOR %%i IN (C:\Users\sneha\Desktop\julyFolder\*.xlsx) DO (

For /F "tokens=1-4 delims=/ " %%A in ("%DATE%") do (
Set All=%%D%%C%%B
)
For /F "tokens=1-4 delims=:. " %%A in ("%time%") do (
Set Allm=%%A%%B%%C%%D
)

move "%%i" C:\Users\sneha\Desktop\desti\sneha_!All!!Allm!.xlsx
)

::DONE

I made three "real" changes, the rest were cosmetic:
got rid of the goto and the :loop
swapped ! for % in the final move, around the date/time variables all, allm
added the fractional seconds value to allm to keep from overwriting or otherwise
conflicting in case where the "seconds" value is same as previous cycle (filename).
hopefully this will work ;-)



#1
August 5, 2012 at 22:23:02
get rid of the "goto" inside the ( ). Not sure why you want that test: N geq Limit.
get rid of this:
if !N! geq %limit% GOTO :LOOP

I'm willing to bet that's fouling the gears.


Report •

#2
August 5, 2012 at 23:21:18
After removing that line i am getting only one single file in destination even if m havn 15 files in source. & too not in correct format. :(

After running the batch file i lost all my files from source n got a single file in desti :(


Report •

#3
August 5, 2012 at 23:22:31
where should i put N geq Limit ??

Report •

Related Solutions

#4
August 6, 2012 at 12:07:42
✔ Best Answer
well, no-where. You don't need it. (besides, n and limit are always equal anyway). Try this re-built version:

@echo off & setLocal EnableDelayedExpansion

FOR %%i IN (C:\Users\sneha\Desktop\julyFolder\*.xlsx) DO (

For /F "tokens=1-4 delims=/ " %%A in ("%DATE%") do (
Set All=%%D%%C%%B
)
For /F "tokens=1-4 delims=:. " %%A in ("%time%") do (
Set Allm=%%A%%B%%C%%D
)

move "%%i" C:\Users\sneha\Desktop\desti\sneha_!All!!Allm!.xlsx
)

::DONE

I made three "real" changes, the rest were cosmetic:
got rid of the goto and the :loop
swapped ! for % in the final move, around the date/time variables all, allm
added the fractional seconds value to allm to keep from overwriting or otherwise
conflicting in case where the "seconds" value is same as previous cycle (filename).
hopefully this will work ;-)


Report •

Ask Question