Batch program to rename and delete files - windows

I am trying to check if there any files with *.del extension in c:\temp1 directory. If found, I need to rename such files to .done in the same x directory and delete the same file present in y directory but they will have .gz extension, please suggest. I am using the script below, but when I run it, it says file not found.
Inputfilename : 20130216.001_visual_sciences_web_feed.out.del
Renamedfilename: 20130216.001_visual_sciences_web_feed.out.done
Filetobedeleted: 20130216.001_visual_sciences_web_feed.out.gz
The script:
#echo off
set "dir=c:\raja\temp1"
set "ext=del"
set "rename=.done"
for /f "delims=" %%a in ('dir /b /a-d /s "%dir%\*.%extension%"^|sort /r') do (
echo FILE: %%~fa
call :rename "%%~fa"
)
pause
goto :eof
Please suggest a solution.

for %%a in ("c:\raja\temp1\*.del") do (
ren "%%~fa" *.done >nul
if exist "c:\temp2\%%~na.gz" del "c:\temp2\%%~na.gz" >nul
)
For each file with .del extensions in source, rename to .done , and if file with same name and .gz extension exists (in temp2, from comments) , delete it

Related

search the requested file in subfolders and copy to destination

I am trying to copy the listed files in file_list.txt to destination folder from source location, source folder has subfolders. my batch should be capable to search the file in source subfolders and copy to destination folder. same for copy all files with extension .exe. what is wrong with my code. I think, I have missed to search subfolders data. don't know what is the command. please help.
#Echo Off
SETLOCAL ENABLEDELAYEDEXPANSION
#color 0a
cls
set "dest=D:\destination"
set /p source=Select source path:
for /R %%f in (%source%/*.exe) do copy %%f "%dest%"
echo Copy requested files
for /f %%f in (file_list.txt) do (
for /f "tokens=*" %%F IN ('dir /S /B /A:-D "%source%/%%f"') Do (
copy "%%F" "%dest%\%USERNAME%"
)
)
pause
ENDLOCAL
I cannot see anything obviously wrong with the code you've posted, although there are possibilities for errors. You have not provided sufficient information, about your current directory, the content of the text file, how the script is invoked or any debugging information.
The following version of your code, requires that you put your file listing text file into the variable definition on line five. I've assumed that the batch file is in the same location as the file listing, and therefore used %~dp0. If it is in the current directory instead, then replace that with %CD%\, and obviously the fully qualified absolute path if neither.
Next I use some validation to try to ensure that the source, destination, and user input exist. The input location will then become the current directory.
Your provided commands are then run, before the curent directory is returned to its original location.
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Color 0A
Set "listdir=%~dp0filelist.txt"
Set "dest=D:\destination"
If Not Exist "%listdir%" GoTo :EOF
If Not Exist "%dest%\" GoTo :EOF
:Ask
ClS
Set "source="
Set /P "source=Select source path: "
If Not Defined source GoTo Ask
PushD "%source:"=%" 2> NUL || GoTo Ask
For /R %%G In (*.exe) Do Copy /Y "%%G" "%dest%"
Echo Copy requested files
For /F "UseBackQ EOL=? Delims=" %%G In ("%listdir%"
) Do For /F "Delims=" %%H In ('Dir "%%~nxG" /S /B /A:-D 2^> NUL'
) Do Copy /Y "%%H" "%dest%\%UserName%"
PopD
Pause
Feel free to try the code, and provide some proper debugging information if it still fails to work as intended.

How to copy several specified files from a source folder and its subfolders to a destination folder?

I have source folder: c:\prefix\bin
I want to copy a set of specified files from source folder and its subfolders.
Lets say I want to copy:
bin
... gtsam.dll
... msvcr120.dll
... intel64\
... vc12\
... tbb.dll
To be more clearly for what I want to copy:
.\gtsam.dll .\msvcr120.dll .\intel64\vc12\tbb.dll
There are many files in the source directory and many subdirectories that I don't want to copy. And all the specified files I have to copy do not share a wild card. They are not all *.dll to copy to c:\dst
How I can do it with the most elegant way?
Using copy, or xcopy, or robocopy?
I suggest to create first a simple text file containing line by line the files to copy with relative path.
Example for FilesList.txt:
gtsam.dll
msvcr120.dll
intel64\vc12\tbb.dll
It is up to you in which directory to store this list file with the names of the files to copy.
The code below expects this file in directory C:\prefix\bin.
Then create a batch file with following code:
#echo off
pushd "C:\prefix\bin"
for /F "usebackq delims=" %%F in ("FilesList.txt") do (
%SystemRoot%\System32\xcopy.exe "%%~F" C:\dst\ /C /H /I /K /Q /R /Y >nul
)
popd
If target is specified with a backslash at end as done here and option /I is used, console application xcopy expects that C:\dst is a directory and even creates the entire directory structure to this directory automatically if not already existing.
Or you use this script with command copy and making sure the destination directory exists before copying the files.
#echo off
if not exist "C:\dst" (
md "C:\dst"
if errorlevel 1 (
echo.
echo Failed to create directory C:\dst
echo.
pause
goto :EOF
)
)
pushd "C:\prefix\bin"
for /F "usebackq delims=" %%F in ("FilesList.txt") do (
copy /B /Y "%%~F" C:\dst\ >nul
)
popd
Command md creates also entire directory tree on creating a directory with command extensions enabled as by default.
In both cases the directory C:\dst contains after batch file execution:
gtsam.dll
msvcr120.dll
tbb.dll
The main advantage of using a list file containing the names of the files to copy with relative path is easy updating in future without the need to change the batch code.
But let's say the files should be copied with duplicating the directory structure from source to destination directory resulting in directory C:\dst containing after batch file execution:
gtsam.dll
msvcr120.dll
intel64
vc12
tbb.dll
In this case the batch code with xcopy could be:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
pushd "C:\prefix\bin"
if "%CD:~-1%" == "\" ( set "BasePath=%CD%" ) else ( set "BasePath=%CD%\" )
for /F "usebackq delims=" %%F in ("FilesList.txt") do (
set "SourcePath=%%~dpF"
set "RelativePath=!SourcePath:%BasePath%=!"
%SystemRoot%\System32\xcopy.exe "%%~F" "C:\dst\!RelativePath!" /C /H /I /K /Q /R /Y >nul
)
popd
endlocal
And the batch code using copy could be:
#echo off
if not exist "C:\dst" (
md "C:\dst"
if errorlevel 1 (
echo.
echo Failed to create directory C:\dst
echo.
pause
goto :EOF
)
)
setlocal EnableDelayedExpansion
pushd "C:\prefix\bin"
if "%CD:~-1%" == "\" ( set "BasePath=%CD%" ) else ( set "BasePath=%CD%\" )
for /F "usebackq delims=" %%F in ("FilesList.txt") do (
set "SourcePath=%%~dpF"
set "RelativePath=!SourcePath:%BasePath%=!"
md "C:\dst\!RelativePath!" 2>nul
copy /B /Y "%%~F" "C:\dst\!RelativePath!" >nul
)
popd
endlocal
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
copy /?
echo /?
endlocal /?
if /?
for /?
goto /?
md /?
pause /?
popd /?
pushd /?
set /?
setlocal /?
xcopy /?
Not tested:
#echo off
set "basedir=c:\prefix\bin"
set "destination=c:\destination"
for /r "%basedir%" %%# in (*gtsam.dll *msvcr120.dll *tbb.dll) do (
copy "%%~f#" "%destination%"
)

Adding Parent Directory name as Prefix to sub directory files using batch file

Adding Parent Directory name as Prefix to sub directory files using batch file
I need to add the prefix of the parent directory to all the files present in sub directories
for example we receive html and text files within Directories 101 as parent directory and Creatives as sub directory
F:\Files\101\Creatives\filename.htm and filename.txt
F:\Files\102\Creatives\filename.htm and filename.txt
F:\Files\103\Creatives\filename.htm and filename.txt
here I want to omit the sub directory name (Creatives) and add 101_, 102_, 103_ as the prefix to the filenames present in sub directories
example 101_filename.htm also for the text file as 101_filename.txt in sub folders
with the below code I can add the prefix to the sub folder files but I can only replace it with a static value (Prefix_), here I need to add the first directory name(101, 102, etc) as prefix to all the files present in (F:\Files) folder
#echo off
pushd "F:\Files"
for /r %%j in (*) do (
rename "%%j" "Prefix_%%~nxj"
)
popd
#echo off
pushd "F:\Files"
for /d %%P in (*) do for /f "delims=" %%F in ('dir /b /s /a-d "%%P"') do rename "%%F" "%%P_%%~nxF"
popd
The inner loop is FOR /F with DIR command instead of FOR /R to eliminate possibility of loop renaming the same file twice.
I guess you could find a solution involving 2 for loop.
Something like
for /f "tokens=*" %%d in ('dir /b') do ( for /f "tokens=*" %%f in ('dir /s /b %%d') do ( rename "%%f" "%%d_%%~nxf" ) )
Best Regards,
Michel.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "source=u:\Files"
pushd "%source%"
for /r %%j in (*) do (
SET "prefix=%%~dpj"
CALL :setpfx
ECHO rename "%%j" "!Prefix!_%%~nxj"
)
popd
GOTO :EOF
:setpfx
SET prefix=!prefix:*%source%=!
SET prefix=%prefix:\=.%
FOR /f %%t IN ("%prefix%") DO SET prefix=%%~nt
SET prefix=%prefix:.=_%
SET "prefix=%prefix:~1%"
GOTO :eof
This may do what you require. The required commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO RENAME to REN to actually rename the files.

Windows cmd sort files by file type and store them in folder

I would like to have a batch file wich will sort files by file type and sorts them into folder.
For example I will run this batch file in some folder and .PDF files will be saved in "PDF" folder, same to do with other file types. Is possible to do that in command line?
Thank you.
Please put the code below in a .bat file and save it to your folder with files and run it.
#echo off
rem For each file in your folder
for %%a in (".\*") do (
rem check if the file has an extension and if it is not our script
if "%%~xa" NEQ "" if "%%~dpnxa" NEQ "%~dpnx0" (
rem check if extension forlder exists, if not it is created
if not exist "%%~xa" mkdir "%%~xa"
rem Copy (or change to move) the file to directory
copy "%%a" "%%~dpa%%~xa\"
)
)
Try this:
#echo off
setlocal enabledelayedexpansion
for %%a in (*.*) do (
set "fol=%%~xa" & set "fol=!fol:.=!"
if not exist !fol! md !fol!
for /f %%b in ('dir /on /b *.*') do (
if %%~xb EQU .!fol! move %%~nxb !fol!
)
)
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET destdir=c:\destdir
SET "extdone=:"
FOR /f "delims=" %%a IN ('dir /b /a-d') DO (
SET ext=%%~xa
IF DEFINED ext (
SET extdone|FIND /i ":%%~xa:" >NUL
IF ERRORLEVEL 1 (
SET extdone=:%%~xa: !extdone!
IF EXIST "%destdir%\!ext:~1!" (
ECHO MOVE "*%%~xa" "%destdir%\!ext:~1!"
) ELSE (ECHO no MOVE "%%~xa")
)
)
)
GOTO :EOF
This batch should do as you ask.
The required commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO MOVE to MOVE to actually move the files.
I've assumed that you only wish to move the files if destinationdirectory\extensionfound exists. If you want to create a directory for a new extension, simply add a line
md "%destdir%\!ext:~1!" 2>nul
after the SET extdone=:%%~xa: !extdone! line.

dos script to find some files , create a folder in the same directory and move them in

i want to create a dos script (.bat) to search on all sub folders and whenever it finds a file with the word MK11 in the file name it must create a folder named archive and move the file in it.
example:
c:\folder1\folder2\folderX\fileMK11.txt -> c:\folder1\folder2\folderX\archive\fileMK11.txt
c:\folder1\folder3\fMK11ile.txt -> c:\folder1\folder3\archive\fMK11ile.txt
I tried to make the following script from examples i have seen but the problem is that it creates the folder "archive" in the directory where the script is instead of the directory where the file is found.
setlocal ENABLEDELAYEDEXPANSION
set /a c=0
FOR /R %%i in (*MK11*) do (
set /a c=c+1
md archive
move "%%i" archive
)
endlocal
I think this script will get you down the road. I echoed a COPY command rather than a MOVE command, but some of the hard part is done.
#echo off
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
SET TEMPFILE=%TEMP%\afinder_%RANDOM%_%RANDOM.tmp
DIR /S /B /A-D *MK11* >%TEMPFILE%
FOR /F "usebackq delims=" %%f IN (`type %TEMPFILE%`) DO (
ECHO "%%f"
FOR /F "delims=\ tokens=*" %%a IN ("%%f") DO (
SET PNAME="%%~pa"
ECHO PNAME is set to !PNAME!
ECHO "!PNAME:~-9,7!"
REM Check to see if this file is already in an Archive directory.
IF "!PNAME:~-9,7!" == "Archive" (
echo got one
) else (
echo not one
IF NOT EXIST "!PNAME!\Archive" (MKDIR "!PNAME!\Archive")
echo COPY %%f "!PNAME!\Archive"
)
)
)
IF EXIST "%TEMPFILE%" (DEL "%TEMPFILE%")
EXIT /B 0

Resources