Return to Snippet

Revision: 58249
at July 4, 2012 06:39 by _reydin_


Updated Code
@echo off
TITLE Add Date

set RENAMED="%userprofile%\Desktop\Output_Renamed"
echo This batch file will copy all the files from a directory and save them 
echo to %RENAMED%. It will also add the current 
echo date to the files' name.
ECHO.
ECHO.
echo For questions about this program please contact Corey.
ECHO.
ECHO.
pause
goto START

:START
cls
set Loc="%userprofile%\Desktop\Input_Renamed"
echo. 
echo Your current working directory is %Loc%
set /p INPUT=Would you like to use this directory? y/n or q for quit:  
echo. 

REM Get, cut and set the date variable. 
for /f "tokens=2-4 delims=/- " %%a in ('date /t') do set XDate=%%a%%b%%c

REM Depending on the user's answer from above do the following.
IF /i %INPUT% == y goto CURRENT 
IF /i %INPUT% == n goto OTHER
IF /i %INPUT% == q goto END 
IF /i %INPUT% NEQ y goto INVALID
IF /i %INPUT% NEQ n goto INVALID
IF /i %INPUT% NEQ q goto INVALID

REM Rename the files in the current working directory. 
:CURRENT
IF NOT EXIST %Loc% goto NOINPUTDIR
REM Create the Renamed_Files directory on the user's desktop if it does not exist. 
IF NOT EXIST %RENAMED% mkdir %RENAMED%
cd %Loc%
for /f "tokens=*" %%b in ('dir /b') do (
copy "%%b" %RENAMED%
)
ECHO.

REM 1st way of renaming. Works if there is only one period.
REM ren *.* ??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????_%XDate%*.*

REM 2nd way of renaming. Put the date on the front of the filename.
REM for /f "tokens=*" %%b in ('dir /b') do (
REM ren "%%b" "%XDate%

REM 3rd way of renaming
cd %RENAMED%
for /f "tokens=*" %%b in ('dir /b') do (
ren "%%b" "%%~nb_%XDate%%%~xb"
)
dir /b
goto END

REM Rename the files in another specified directory.
:OTHER
cls
set /p DIRPATH=Enter the full path of the directory or q to quit:  
IF /i %DIRPATH% == q goto END
IF /i %DIRPATH% == c: goto WINDOWS
IF /I %DIRPATH% == c:\ goto WINDOWS  
IF /i %DIRPATH% == %WINDIR% goto WINDOWS

echo.
echo You entered %DIRPATH%
set /p CORPATH=Is this correct? y/n or q for quit:  
ECHO.

REM Do the following depending on the user's entry.
IF /i %CORPATH% == y goto RENOTHER
IF /i %CORPATH% == n goto OTHER
IF /i %CORPATH% == q goto END 
IF /i %CORPATH% NEQ y goto INVALID
IF /i %CORPATH% NEQ n goto INVALID
IF /i %CORPATH% NEQ q goto INVALID

REM This part is used to rename files that are located in another directory.
:RENOTHER
IF NOT EXIST %DIRPATH% goto NOTTHERE
IF NOT EXIST %RENAMED% mkdir %RENAMED%
cd %DIRPATH%
for /f "tokens=*" %%b in ('dir /b') do (
copy "%%b" %RENAMED%
)
ECHO.
cd %RENAMED%
for /f "tokens=*" %%b in ('dir /b') do (
ren "%%b" "%%~nb_%XDate%%%~xb"
)
echo.
dir /b
goto END

:WINDOWS
ECHO.
ECHO This is the Windows directory. Can't rename.
ECHO.
goto OTHER

:NOTTHERE 
echo. 
ECHO The directory could not be found!
echo. 
set /p TRYAGAIN=Would you like to try again? y/n  
IF /i %TRYAGAIN% == y goto OTHER
IF /i %TRYAGAIN% == n goto END
IF /i %TRYAGAIN% NEQ y goto INVALID
IF /i %TRYAGAIN% NEQ n goto INVALID

:INVALID
echo. 
echo Your input was not recognized!
pause
goto END

:NOINPUTDIR
cls
ECHO I'm sorry but your Input_Renamed directory does not exist.
ECHO.
ECHO You must create Input_Renamed directory on your Desktop and copy the files you
ECHO want to rename to that directory. Otherwise, please specify a different 
ECHO directory to use. 
ECHO. 
set /p TRYAGAIN=Would you like me to create the Input_Renamed directory? y/n or d to specify another directory:   
IF /i %TRYAGAIN% == y goto INPUTDIRCREATE
IF /i %TRYAGAIN% == n goto END
IF /i %TRYAGAIN% == d goto OTHER
IF /i %TRYAGAIN% NEQ y goto INVALID
IF /i %TRYAGAIN% NEQ n goto INVALID
IF /i %TRYAGAIN% NEQ d goto INVALID

:INPUTDIRCREATE
cls
mkdir %Loc%
ECHO Input_Renamed has been created on your Desktop. Please copy the files to 
ECHO that directory or specify another directory.
goto START


:END
echo.
echo.
echo Exiting the program, HAVE A NICE DAY!
echo.
pause

Revision: 58248
at July 4, 2012 06:36 by _reydin_


Initial Code
@echo off
TITLE Salmons PC

set RENAMED="%userprofile%\Desktop\Output_Renamed"
echo This batch file will copy all the files from a directory and save them 
echo to %RENAMED%. It will also add the current 
echo date to the files' name.
ECHO.
ECHO.
echo For questions about this program please contact Corey.
ECHO.
ECHO.
pause
goto START

:START
cls
set Loc="%userprofile%\Desktop\Input_Renamed"
echo. 
echo Your current working directory is %Loc%
set /p INPUT=Would you like to use this directory? y/n or q for quit:  
echo. 

REM Get, cut and set the date variable. 
for /f "tokens=2-4 delims=/- " %%a in ('date /t') do set XDate=%%a%%b%%c

REM Depending on the user's answer from above do the following.
IF /i %INPUT% == y goto CURRENT 
IF /i %INPUT% == n goto OTHER
IF /i %INPUT% == q goto END 
IF /i %INPUT% NEQ y goto INVALID
IF /i %INPUT% NEQ n goto INVALID
IF /i %INPUT% NEQ q goto INVALID

REM Rename the files in the current working directory. 
:CURRENT
IF NOT EXIST %Loc% goto NOINPUTDIR
REM Create the Renamed_Files directory on the user's desktop if it does not exist. 
IF NOT EXIST %RENAMED% mkdir %RENAMED%
cd %Loc%
for /f "tokens=*" %%b in ('dir /b') do (
copy "%%b" %RENAMED%
)
ECHO.

REM 1st way of renaming. Works if there is only one period.
REM ren *.* ??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????_%XDate%*.*

REM 2nd way of renaming. Put the date on the front of the filename.
REM for /f "tokens=*" %%b in ('dir /b') do (
REM ren "%%b" "%XDate%

REM 3rd way of renaming
cd %RENAMED%
for /f "tokens=*" %%b in ('dir /b') do (
ren "%%b" "%%~nb_%XDate%%%~xb"
)
dir /b
goto END

REM Rename the files in another specified directory.
:OTHER
cls
set /p DIRPATH=Enter the full path of the directory or q to quit:  
IF /i %DIRPATH% == q goto END
IF /i %DIRPATH% == c: goto WINDOWS
IF /I %DIRPATH% == c:\ goto WINDOWS  
IF /i %DIRPATH% == %WINDIR% goto WINDOWS

echo.
echo You entered %DIRPATH%
set /p CORPATH=Is this correct? y/n or q for quit:  
ECHO.

REM Do the following depending on the user's entry.
IF /i %CORPATH% == y goto RENOTHER
IF /i %CORPATH% == n goto OTHER
IF /i %CORPATH% == q goto END 
IF /i %CORPATH% NEQ y goto INVALID
IF /i %CORPATH% NEQ n goto INVALID
IF /i %CORPATH% NEQ q goto INVALID

REM This part is used to rename files that are located in another directory.
:RENOTHER
IF NOT EXIST %DIRPATH% goto NOTTHERE
IF NOT EXIST %RENAMED% mkdir %RENAMED%
cd %DIRPATH%
for /f "tokens=*" %%b in ('dir /b') do (
copy "%%b" %RENAMED%
)
ECHO.
cd %RENAMED%
for /f "tokens=*" %%b in ('dir /b') do (
ren "%%b" "%%~nb_%XDate%%%~xb"
)
echo.
dir /b
goto END

:WINDOWS
ECHO.
ECHO This is the Windows directory. Can't rename.
ECHO.
goto OTHER

:NOTTHERE 
echo. 
ECHO The directory could not be found!
echo. 
set /p TRYAGAIN=Would you like to try again? y/n  
IF /i %TRYAGAIN% == y goto OTHER
IF /i %TRYAGAIN% == n goto END
IF /i %TRYAGAIN% NEQ y goto INVALID
IF /i %TRYAGAIN% NEQ n goto INVALID

:INVALID
echo. 
echo Your input was not recognized!
pause
goto END

:NOINPUTDIR
cls
ECHO I'm sorry but your Input_Renamed directory does not exist.
ECHO.
ECHO You must create Input_Renamed directory on your Desktop and copy the files you
ECHO want to rename to that directory. Otherwise, please specify a different 
ECHO directory to use. 
ECHO. 
set /p TRYAGAIN=Would you like me to create the Input_Renamed directory? y/n or d to specify another directory:   
IF /i %TRYAGAIN% == y goto INPUTDIRCREATE
IF /i %TRYAGAIN% == n goto END
IF /i %TRYAGAIN% == d goto OTHER
IF /i %TRYAGAIN% NEQ y goto INVALID
IF /i %TRYAGAIN% NEQ n goto INVALID
IF /i %TRYAGAIN% NEQ d goto INVALID

:INPUTDIRCREATE
cls
mkdir %Loc%
ECHO Input_Renamed has been created on your Desktop. Please copy the files to 
ECHO that directory or specify another directory.
goto START


:END
echo.
echo.
echo Exiting the program, HAVE A NICE DAY!
echo.
pause

Initial URL


Initial Description
This is a batch file that will add the date to the end of all files in a particular directory located on the user's desktop and place them in a directory called Output_Renamed on the user's desktop.

Initial Title
Add a current date to a file

Initial Tags
date, file

Initial Language
DOS Batch