Windows batch to add prefix to file names, why added twice? - windows

In order to add a simple "hello" prefix to all pdf files in a folder I'm using this batch file:
FOR %%F IN (*.pdf) DO (RENAME "%%F" "hello%%F")
Saved this into a "rename.bat" file and placed it into the folder I need the files to be renamed. Then I just double click on "rename.bat".
This almost works but the 1st file gets the prefix added twice.
Let's say in the folder I have: A.pdf, B.pdf, C.pdf, they get converted into:
hellohelloA.pdf
helloB.pdf
helloC.pdf,
Do you know what's wrong in the batch file?
I noticed it always does this when files are more than one. It works ok when there is only one file in the folder, but it is not very useful :-).

/f removes the issue of recapturing an existing file:
FOR /f "delims=" %%F IN ('DIR /a-d /b *.pdf') DO (RENAME "%%F" "hello%%F")

#echo off
echo.
echo. Add Whatever Prefix...
echo.
echo. You Want To Add...
echo.
echo. To The Filename...
echo.
set /p variable=" > "
setlocal enabledelayedexpansion
for /f "delims=" %%a in (' dir /b /a-d *.pdf') do (
set oldName=%%a
Set newName=%variable%!oldName!
Ren "!oldName!" "!newName!"
)
exit
This works well..... Try It Out ... No Double Prefix... Ever.

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.

Batch file to search directory for list of files and copy to single folder

I have a .txt file with a list of files that are on a network drive (M:). I need a batch file to go through that list, search for the files which are in sub-directories of a single folder on that drive, and then copy them to another folder. I have tried quite a few solutions with no luck.
the text file is a list of files and extensions i.e.
abc.step
afer.iges
ca76dc7d.sldprt
Here's what I tried so far
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
cls
set dest=C:\Users\kduquette.000\Desktop\Files
for /f "TOKENS=*" %%f in (C:Desktop\Files\list.txt) do (
set i=1
for /f "tokens=*" %%F IN ('dir M: "%%~f"') Do (
for %%N in ("%%F") Do (
set name=%%~NN
set ext=%%~XN
)
copy "%%~F" "%dest%\!name!_!i!!ext!"
set /A i=!i!+1
)
)
ENDLOCAL
We can just do a check if file exists and copy it:
#echo off
set "dest=C:\Users\kduquette.000\Desktop\Files"
for /f "delims=" %%i in (C:\Desktop\Files\list.txt) do if exist "M:\Cads\%%i" echo copy "M:\Cads\%%i" "%dest%"
Note, for now, I added echo to the copy string, so you can test the result first. Once happy with the results, remove echo
If however you want to search for the files, recursively throughout M:\ drive then:
#echo off
set "dest=C:\Users\kduquette.000\Desktop\Files"
for /f "delims=" %%i in (C:\Desktop\Files\list.txt) do (
pushd M:\
for /f "delims=" %%a in ('dir /b /s "%%i"') do echo copy "%%~fa" "%dest%"
popd
)

Search for files based on a list of partial names and copy them to a destination folder using windows shell

I am an absolute newbie to batch programming. I have posted this question after some amount of searching. Kindly guide me.
I have a folder containing a thousand images:
000001_x_abc1.jpg
000001_x_efg1.jpg
000001_x_hij1.jpg
000002_x_abc1.jpg
000002_x_efg1.jpg
000002_x_hij1.jpg
.
.
.
.
234562_x_abc2.jpg
234562_x_efg2.jpg
234562_x_hij2.jpg
Of theses files I have generated a 'list of files' that I need to pull out based on partial names i.e the numeric ID - first 6 numeric values in the file name e.g 234562*.jpg and copy them to a destination folder.
Note: Every numeric ID based search should give me 3 files and I need to copy all three. Any help would be appreciated.
I have tried the following code based on my search:
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "DEST_DIR=my_desination"
SET "SEARCH_DIR=my_source"
FOR /F "tokens=*" %%a IN (%~dp0my_list.txt%) DO (
FOR /R "%SEARCH_DIR%" %%f IN (*%%a*) DO (
SET "SRC=%%~dpf"
SET DEST=!SRC:%SEARCH_DIR%=%DEST_DIR%!
xcopy /S /I "%%~f" "!DEST!"
)
)
And my list file is as below:
002631_*.jpg
054741_*.jpg
054992_*.jpg
055053_*.jpg
055054_*.jpg
055118_*.jpg
055267_*.jpg
055294_*.jpg
055382_*.jpg
055415_*.jpg
055466_*.jpg
055546_*.jpg
This is an example of copying specific files to a folder.
#ECHO OFF
set "SOURCE_DIR=%userprofile%\Desktop\Source"
set "DEST_DIR=%userprofile%\Desktop\Output"
set "FILENAMES_TO_COPY=test.txt test1.txt test2.txt"
pushd "%SOURCE_DIR%"
for %%F IN (%FILENAMES_TO_COPY%) do (
echo file "%%F"
xcopy /Y "%%F" "%DEST_DIR%\"
)
popd
pause
The script copies test.txt test1.txt and test2.txt from the folder Source, to the folder Output
Check out this page
Supposing the list file contains full file patterns one per line, the following should work for you:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Define constants here:
set "SOURCEDIR=D:\Data"
set "TARGETDIR=D:\BackUp"
set "LISTFILE=D:\files.lst"
cd /D "%SOURCEDIR%" || exit /B 1
for /F "usebackq delims= eol=|" %%L in ("%LISTFILE%") do (
for /F "delims= eol=|" %%F in ('dir /B "%%L"') do (
copy "%%~F" "%TARGETDIR%\%%~nxF"
)
)
endlocal
exit /B
(edited to reflect additional information in the question)
for /f %%a in (partial.txt) do copy %%a "x:\destination folder\"
for every entry in the textfile copy <entry from textfile> to the destination.
see for /? for more details

Batch remove parenthesis from file name

After successfully removing a bunch of Google Drive Folder duplicates, some files retain a "filename(2)"name.
Is there a way to batch rename every file so the parenthesis and the number inside the parenthesis is gone?
That includes folders and sub-folders.
Try like this :
Create a file test.bat with the code below in it and replace the path to test in the var $path
#echo off
set $path="C:\Users\CN Micros\Desktop\PROGRAMMATION\test"
for /f "tokens=1-3 delims=^(^)" %%a in ('dir /b/a-d %$path%') do (
if exist %$path%\"%%a(%%b)%%c" echo ren %$path%\"%%a(%%b)%%c" "%%a%%c"
)
pause
Then run it in the CMD or by double clicking.
If the output is ok for you remove the echo
The program create 3 tokens : %%a = what's before the (), %%b What's inside the () and %%c what's after the ().
Then we arrange this 3 tokens to rename the files without the ().
If you have some file who have the same final name ie : "file(1)name", "file(2)name" --> "filename"
It will work only with the first one. If you have this case you have to add a counter at the end of file to be sure that they will be renamed.
This will create renfiles.bat.txt for you to examine in Notepad and then rename to .bat and execute if you are happy with it.
#echo off
dir /b /a-d *(*).* |find /i /v "%~nx0" |find /i /v "repl.bat" |repl "(.*)\(.*\)(\..*)" "ren \q$&\q \q$1$2\q" xa >"renfiles.bat.txt"
This uses a helper batch file called repl.bat - download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat
Place repl.bat in the same folder as the batch file or in a folder that is on the path.
Edit: This version will recurse through subdirectories:
#echo off
dir /b /s /a-d *(*).* |find /i /v "%~nx0" |find /i /v "repl.bat" |repl ".*\\(.*)\(.*\)(\..*)" "ren \q$&\q \q$1$2\q" xa >"renfiles.bat.txt"
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
FOR /f "delims=" %%a IN (
'dir /b /s /a-d "%sourcedir%\*" '
) DO (
SET "name=%%~na"
SETLOCAL ENABLEDELAYEDEXPANSION
SET "newname=!name:)=!"
SET "newname=!newname:(=!"
IF "!name!" neq "!newname!" (
IF EXIST "%%~dpa!newname!%%~xa" (ECHO cannot RENAME %%a
) ELSE (ECHO(REN "%%a" "!newname!%%~xa")
)
endlocal
)
GOTO :EOF
You'd need to set your required directory into sourcedir. I used u:\sourcedir which suits my testing.
The required REN commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(REN to REN to actually rename the files.

Remove Prefix from all file in current folder as well as files in subfolder windows batch

i am trying to remove prefix from all files in current folder and subfolders
i tryed following code which work only for current folder
setlocal enabledelayedexpansion
for %%F in (*) do (
set "FN=%%F"
set "FN=!FN:~15!"
ren "%%F" "!FN!"
)
goto :eof
Please Help me to solve this
for /f "delims=" %%a in ('dir /b /a-d /s') do (
set "fname=%%~nxa"
set "fpath=%%~dpa"
setlocal enabledelayedexpansion
set "nname=!fname:~15!"
ren "!fpath!!fname!" "!nname!"
endlocal
)
This is the safe way to preserve exclamation marks.
If you are using windows 7, you could try this:
forfiles /s /c "cmd /c ren #file #fname"
It took me a bit of time to find, but suddenly I realised that the batch file was not working because it had renamed itself!.
If this becomes an issue you could try naming the batch file zzzzzzzzz.bat which I think would prevent it from renaming itself first.
Mona

Resources