Batch script for renaming files - windows

I need help regarding how to rename files automatically from a subfolders in the C:\temp_download which contains res_save.0 in those folders. I want to rename it as nvm_res.0 and then subsequently nvm_res.1... nvm_res.2. When copying them to the folder C:\temp, it keep replacing. Can you help me with the wildcards? the code is below.
for /R "C:\temp_download" %%f in (res_save.*) do xcopy "%%f" "C:\temp\num_res.*" /Y

When you get an answer that solves your issue, make sure you accept the answer so that future searchers get an idea of which solution to try first.
This might do what you need.
#echo off
set c=0
setlocal enabledelayedexpansion
for /R "C:\temp_download" %%f in (res_save.*) do (
set /a c+=1
copy "%%f" "C:\temp\nvm_res.!c!" /Y >nul
)

Related

how to move folders with a loop over folders (in batch)?

Situation:
I try to move files inside a loop in shell but my code is not working.
for /D %%F in (*) do (
if "%%F" NEQ "%directoryToPutFilesIn%" (
move /y "%%F" "%directoryToPutFilesIn%"
)
)
After hours of testing it, I realized it's because %%F is pointing to the folder, hence the file cannot be moved.
Bad solution:
The way I made it work and confirmed my suspicions is by saving the value of %%F in another variable and using that variable on next turn to move the file. Note, what is following needs an initialisation of %precedentFile% for the first turn.
for /D %%F in (*) do (
move /y "%precedentFile%" "%directoryToPutFilesIn%"
if "%%F" NEQ "%directoryToPutFilesIn%" (
move /y "%%F" "%directoryToPutFilesIn%"
set precedentFile=%%F
)
Problem:
This solution is not practical and feels wrongs. Is there a way to adapt my current code to do it, or simply another way?
Try below code to move files from one folder to another in batch script:
for /f %%a in ('dir /a:-D /b') do move /Y "%%~fa" "%directoryToPutFilesIn%"
Explanation :
dir /a:-D /b : This command will list all files in the directory
move /Y "%%~fa" "%directoryToPutFilesIn%" : This will move all files in the directory where this command is executed to the destination you have mentioned.
%%~fa : This command will get full qualified path of the file with it's name.
Try Below code Move directories :
Below command will move directories in the Path where this command is executed to the destination provided. In this that will be H:\ Drive, Change it accordingly
for /D %%b in (*) do move /Y "%%~fb" "H:\"

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

Make subfolder names from part of file name and copy files with Robocopy

Is it posible to copy and make directories automatically from file name substrings using Robocopy?
I mean i have files like these. LAJ00306130201004626.rc the first 8 chararacters are control number (LAJ00306=control number) this would be the name of the folder and the rest are the date and time (Date=130201) (time=004626).
LAJ00306130201004626.rc
LAJ00306130202004626.rc
LAJ00306130203004626.rc
LAJ00307130201004626.rc
LAJ00307130202004626.rc
and i would like to copy and create folders from the file name like under and copy the files mentioned before in the new folders.
LAJ00306
LAJ00307
I hope to be clear if necessary ask me for more information
try this, look at the output and remove the echos before MD and ROBOCOPY, if it looks good:
#ECHO OFF &SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcefolder=."
SET "targetfolder=X:\data"
CD /d "%sourcefolder%"
FOR %%a IN (*.rc) DO (
SET "fname=%%a"
SET "folder=!fname:~0,8!"
SET "$!folder!=1"
)
FOR /f "delims=$=" %%a IN ('set "$"') DO (
ECHO MD "%targetfolder%\%%a" 2>nul
ECHO ROBOCOPY "%sourcefolder%" "%targetfolder%\%%a" "%%a*.rc"
)
Set sourcefolder and targetfolder for your folder tree.
Try this:
#echo off
pushd "c:\source folder"
setlocal enabledelayedexpansion
for %%a in (*.rc) do (
set "name=%%a"
robocopy "%cd%" "%%a" "D:\target directory\!name:~0,8!"
)
popd
Answers to your questions are:
pushd "drive:\path" makes the location the current working directory.
popd restores the last working directory
setlocal enabledelayedexpansion allows you to change and use variables within a loop, using the !variable! syntax.
If your 2000 files are in a single folder then it should work - but test it on some sample files first so that you can see how it will work.
#ECHO OFF
SETLOCAL
SET "sourcedir=."
SET "destdir=c:\destdir"
FOR /f "tokens=1*delims=_" %%i IN (
'dir /b /a-d "%sourcedir%\*_*."'
) DO XCOPY /b "%sourcedir%\%%i_%%j" "%destdir%\%%i\"
GOTO :EOF
This should accomplish the task described. You'd need to set up the source and destination directories to suit, of course. Add >nul to the end of the XCOPY line to suppress 'copied' messages.

Batch Rename Subdirectories in Windows

I have a directory with thousands of subdirectories that contain their own subdirectories that need to be renamed. I'm using a Windows 7 machine that I do not have Administrator rights for so I can't download a simple program to do this for me.
Right now I have a test directory C:\test with a few subdirectories that have subdirectories named old that I am trying to change to new using a batch file.
Just to be clear I want the following:
C:\test\1\old
C:\test\2\old
C:\test\3\old
to become
C:\test\1\new
C:\test\2\new
C:\test\3\new
Thank you for any help you can provide.
Here's what I came up with quickly. I ran a quick test locally and it seemed to do what you're asking for:
#echo off
FOR /D %%D IN ("C:\test\*") DO CALL :RENAME %%D
:RENAME
SET CRITERIA=\old
FOR /D %%R IN (%1%CRITERIA%) DO RENAME %%R "new"
Save that to a bat file and give it a shot. Hopefully that helps.
Justin's answer was really helpful for my similar problem, though by default it only handled a simpler pattern for \A\B\C where A is a base directory, B is some undetermined directory and C is the directory you seek.
I modifed his script to recurse the base directory A through any number of layers until it finds C.
Here's the script, written to expect command line paramers:
#echo off
set BASEDIR=%1
SET CRITERIA=\%2
SET REPLACENAME=%3
call :FindDirs %BASEDIR%
GOTO END
:FindDirs
FOR /D %%F IN ("%1\*") DO CALL :RENAME %%F
GOTO:EOF
:RENAME
REM echo DIR=%1
FOR /D %%R IN ("%1%CRITERIA%") DO (
if EXIST %%R RENAME %%R "%REPLACENAME%"
)
call :FindDirs %1
GOTO:EOF

copy and rename files of a certain extension via batch file

I have a folder that has a bunch of files such image_hello.png, helloworld.png, wired.png. I would like to copy these files and then rename them as 1.png, 2.png, 3.png via script or batch file
I am not sure what the best way to start this is, i can copy over the files easily, but after that, i am not sure how to rename them based on the extension.
Any ideas?
Something like this:
#echo off
SET count=1
FOR /f "tokens=*" %%G IN ('dir /b *.png') DO (call :rename_next "%%G")
GOTO :eof
:rename_next
ren "%1" %count%.png
set /a count+=1
GOTO :eof
Take a look here:
http://www.computing.net/answers/windows-xp/batch-file-to-renumber-files-in-folders/181900.html
Something along these lines should work (note: don't have Windows to test):
set n=1
for %%i in (*.png) do (
call ren %%i %%n%%.pn_
set /a n=n+1)
ren *.pn_ *.png
Note that if you only want to do it once, you can use Explorer, as per here:
http://www.mediacollege.com/computer/file/batch-rename/windows-xp.html
or some other utilities, like the one mentioned in the first link:
http://www.snapfiles.com/get/bfrenamer.html

Resources