to rename multiples file in multiple folders using batch - batch-file

I have to write a batch file such that,it has to select the files from various folders as per the date passed in and copy those files to destination and rename the file(ie.if filename is like abcd_yyyymmdd then it will rename as abcd)
Please find below my batch file,it copies all respective files from source to destination as per the date specified,but the renaming is not done.
#ECHO OFF
SET date="20150211"
SET AAH_PATH=D:\ABC\Data\IN\ABCD
COPY D:\ABC\Data\IN\ABCD\*_%date%.txt D:\ABC\Data\File\*.txt
SET FOLDER_PATH=D:\ABC\Data\File
pushd %FOLDER_PATH%
for /F "tokens=1* delims=_" %%a in ('dir /B *.txt') do ren "%%a_%%b" "%%a.txt"
popd
SET SIE_PATH=D:\ABC\Data\IN\SIE
COPY D:\ABC\Data\IN\SIE\*_%date%.txt D:\ABC\Data\NEWFILE\*.txt
SET SIE_DEST=D:\ABC\Data\NEWFILE
pushd %SIE_DEST%
for /F "tokens=1* delims=_" %%a in ('dir /B *.txt') do ren "%%a_%%b" "%%a.txt"
popd
SET UB_ABF=D:\ABC\Data\IN\UB\ABF
COPY D:\ABC\Data\IN\UB\ABF\*_%date%.txt D:\ABC\Data\UB\*.txt
SET UB_BCL=D:\RBC\Data\IN\UB\BCL
COPY D:\RBC\Data\IN\UB\BCL\*_%date%.txt D:\ABC\Data\UB\*.txt
SET UB_DEST=D:\ABC\Data\UB
pushd %UB_DEST%
for /F "tokens=1* delims=_" %%a in ('dir /B *.txt') do ren "%%a_%%b" "%%a.txt"
popd
Could someone help me fix this problem

Try add the file extension to the source path in each for /f ren loop...
for /F "tokens=1* delims=_" %%a in ('dir /B *.txt') do ren "%%a_%%b.txt" "%%a.txt"

Related

Windows CMD for matching files to directories

I have a Windows CMD script that has a bug. The script is supposed to match the first 8 digits (the date) of a file with a directory titled with the same first 8 digits (the date). If successful, the file is moved into that directory & placed in a subfolder (called 'portfolio'). However, the error File not Found is returned.
file: 20230202_example.jpg
directory: 20230202_winter-holiday/portfolio
...the CMD file:
#echo off
for /f "delims=" %%a in ('dir /b /a-d') do (
set "filename=%%a"
set "first8=!filename:~0,8!"
for /f "delims=" %%b in ('dir /b /a-d *%first8%*') do (
if /i "!filename!" neq "%%b" (
move "!filename!" "%%b\portfolio\!filename!"
)
)
)
If I interrogate the directory in Command Prompt:
dir /b /a-d
...I get a full list of the files contained. When the script is run from Command Prompt, for each file contained I get:
File Not Found
Based upon your target file and directory names, you could probably do it without defining variables and therefore the need to delay variable expansion.
Example:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
For /F "Delims=" %%G In ('Dir "????????_*" /A:-D /B 2^>NUL
^| %SystemRoot%\System32\findstr.exe /R /C:"^202[0123]1[012][12][0123456789]_."
/C:"^19[789][0123456789]0[123456789]0[123456789]_."
/C:"^19[789][0123456789]0[123456789][12][0123456789]_."
/C:"^19[789][0123456789]0[123456789]3[01]_." /C:"^202[0123]0[123456789]3[01]_."
/C:"^19[789][0123456789]1[012]0[123456789]_."
/C:"^19[789][0123456789]1[012][12][0123456789]_."
/C:"^19[789][0123456789]1[012]3[01]_." /C:"^20[01][0123456789]1[012]3[01]_."
/C:"^20[01][0123456789]0[123456789]0[123456789]_." /C:"^202[0123]1[012]3[01]_."
/C:"^20[01][0123456789]0[123456789][12][0123456789]_."
/C:"^20[01][0123456789]0[123456789]3[01]_."
/C:"^20[01][0123456789]1[012]0[123456789]_."
/C:"^20[01][0123456789]1[012][12][0123456789]_."
/C:"^202[0123]0[123456789]0[123456789]_." /C:"^202[0123]1[012]0[123456789]_."
/C:"^202[0123]0[123456789][12][0123456789]_."
') Do For /F "Delims=_" %%H In ("%%~nG") Do For /F "Delims=" %%I In ('
Dir "%%H_*" /A:D /B 2^>NUL'
) Do %SystemRoot%\System32\Robocopy.exe . "%%I\portfolio" "%%G" /Mov 1>NUL 2>&1
I decided to use a little more accuracy in the dates, (it's not perfect because it has no knowlege of how many days are in each month of any year, but should cover dates between 19700101 and 20231231). If you don't want that, you could simplify it by just removing lines 4 through 17:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
For /F "Delims=" %%G In ('Dir "????????_*" /A:-D /B 2^>NUL
') Do For /F "Delims=_" %%H In ("%%~nG") Do For /F "Delims=" %%I In ('
Dir "%%H_*" /A:D /B 2^>NUL'
) Do %SystemRoot%\System32\Robocopy.exe . "%%I\portfolio" "%%G" /Mov 1>NUL 2>&1

Batch file - copy two last modified files

I would like to write a .bat file to move the two last modified files of a specific extension *.bak in directory a to a different directory.
I used this line to copy files:
robocopy D:\DailyBackup\IDMRObjects\SQLBackups SQLBackups *.bak /S
I'm new with this and have no idea how to tweak this to get the result I need.
Thanks
not tested:
#echo off
for /f "tokens=* delims=" %%# in (' dir /a:-d /o:-d /t:a /b "D:\DailyBackup\IDMRObjects\SQLBackups SQLBackups\*.bak"') do (
if not defined last set "pre_last=%%~f#"
set "last=%%~f#"
)
copy /y "%last%" "c:\new_dir"
copy /y "%pre_last%" "c:\new_dir"
#echo off
setlocal EnableDelayedExpansion
cd "D:\DailyBackup\IDMRObjects\SQLBackups"
set copied=0
for /F "delims=" %%a in ('dir /B /A-D /O-D /T:W *.bak') do (
copy "%%a" "other\dir"
set /A copied+=1
if !copied! equ 2 goto break
)
:break

Create BAT file to get directory listing up to 2nd level only

I have a window directory with 300 folders inside of it and 4 levels of folders within each folder. I need a listing of folders only up to 2nd level in a txt file? I need the full path of each directory as well. I would like to use this a BAT file or CMD line prompt. Ex:
Jon Done
Test001
Tester002
Test003
Tester004
Can anyone help me to do this?
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
PUSHD "%sourcedir%"
FOR /f "delims=" %%a IN ('dir /b /a-d 2^>nul') DO ECHO %%~fa
FOR /f "delims=" %%a IN ('dir /b /ad') DO (
PUSHD "%%a"
FOR /f "delims=" %%x IN ('dir /b /a-d 2^>nul') DO ECHO %%~fx
popd
)
popd
GOTO :EOF
This should work - you would need to change the setting of sourcedir to suit your circumstances.

Get inverse list of files through dir command in batch

I'm triying to get a list of files of subfolders in a inverse order using this:
for /f "tokens=*" %%f in ('dir /s /b /o-n') do (echo %%f)
I'm getting this result
folder1\file2, folder1\file, folder2\file2, folder2\file1, folder3\file2, folder3\file1
And I want to get the reverse order in folders and files, not only files of folders. Something like this
folder3\file2, folder3\file1, folder2\file2, folder2\file1\, folder1\file2, folder1\file1
How can I do this?
#echo off
for /f "tokens=*" %%f in ('dir /s /b /o-n') do (echo %%f >> tempfile.123)
C:\Windows\System32\sort.exe /R tempfile.123
del tempfile.123
This just echoes your files to tempporary file and then revereses it. Sorry for the full path to sort.exe but I have cygwin installed. In case you want proper temporary file name I reccomend this http://unserializableone.blogspot.com/2009/04/create-unique-temp-filename-with-batch.html
try this
for /f "tokens=*" %%f in ('dir /s /b /o-n')
do (
SET OUTPUT=%OUTPUT%, %%f
)
echo %OUTPUT%
run a first loop to get first level subdir then run you command on each of them
#echo off
for /f "tokens=*" %%f in ('dir c:\temp\so\ /b /o-n /AD') do (call :filesOf %%f)
:next
echo next
pause
:filesOf
echo "files for ******** %1 *********"
if ("%1"=="") goto next else(
for /f "tokens=*" %%i in ('dir c:\temp\so\%1 /s /b /o-n ') do echo "***%%i***"
)
it will be difficult to handle multi level subdirs tough

How to get the most recent file using a batch file?

I have a list of zip files with date and time appended like yyyymmdd_hhmmss_Demos.zip. Now how to get the most recently added zip file in the source dir. I need to copy this file in the target using copy command.
I found some info about forfiles, but do not have an idea on how to get it done for seconds.
You can use
pushd D:\a
for /f "tokens=*" %%a in ('dir /b /od') do set newest=%%a
copy "%newest%" D:\b
popd
set Path="D:\hello\abc\old"
for /f "tokens=*" %%a in ('dir /A:-D /B /O:-D /S %Path%') do set NEW=%%a&& goto:n
:n
echo %NEW%
pushd \\ryap\CONTROL_DATOS
for /f "tokens=*" %%a in ('dir \\ryap\CONTROL_DATOS /b /od') do set newest=%%a
Xcopy/Y "\\ryap\CONTROL_DATOS\%newest%" "D:\TXT_SOURCES\"
popd
Below snippet will extract the date and customize as per your needs
for /f "tokens=1-4 delims=/ " %%i in ("%date%") do (
set dow=%%i
set month=%%j
set day=%%k
set year=%%l
)
:: Pad digits with leading zeros e.g Sample_01-01-21.csv
set yy=%year:~-2%
set datestr=%day%-%month%-%yy%
Alternate way:
set datestr=%date:~0,2%-%date:~3,2%-%date:~6,2%

Resources