copy and rename files of a certain extension via batch file - vbscript

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

Related

Is there any way for windows batch script or command prompt to list the directories from a certain folder only without including the hard drive?

I tried using dir /b/s *.png to list the directories of all my png files. the results were like this:
D:\Newfolder\test\images\approved\11.png
D:\Newfolder\test\images\approved\12.png
D:\Newfolder\test\images\approved\13.png
D:\Newfolder\test\images\approved\14.png
D:\Newfolder\test\images\approved\15.png
D:\Newfolder\test\images\approved\16.png
D:\Newfolder\test\images\approved\17.png
D:\Newfolder\test\images\approved\18.png
D:\Newfolder\test\images\approved\19.png
D:\Newfolder\test\images\approved\20.png
I want it to be shortened so it will display only starting from the images folder;
images\approved\11.png
images\approved\12.png
images\approved\13.png
images\approved\14.png
images\approved\15.png
images\approved\16.png
images\approved\17.png
images\approved\18.png
images\approved\19.png
images\approved\20.png
is it possible to do?
if it's not, is there any way for me to edit the directories by deleting the first few folder from a generated text file?
say i put dir /b/s *.png > path.txt how do i edit the texts since the list got no whitespaces.
i'm still new to this so i'm not so familiar with much commands but this is as far as my understanding can do.
Assuming this comment in your question:
"I want it to be shortened so it will display only starting from the "images" folder;"
you always want from the images folder, and also assuming the images folder always exists in the path:
#echo off & setlocal enabledelayedexpansion
for /R %%i in (*.png) do (
set "_fp=%%~i"
echo !_fp:*\images\=images\!"
)
pause
Or to explicitly ensure you only include paths with \images\ in the path:
#echo off & setlocal enabledelayedexpansion
for /F "delims=" %%i in ('dir /b /s *.png ^| findstr \images\') do (
set "_fp=%%~i"
echo !_fp:*\images\=images\!"
)
pause
example of using pushd:
#echo off & setlocal enabledelayedexpansion
pushd "%~1"
for /F "delims=" %%i in ('dir /b /s *.png ^| findstr \images\') do (
set "_fp=%%~i"
echo !_fp:*\images\=images\!"
)
pause
popd
So the above you can run from cmd and simply pass the path you want the script to run in as script_name.cmd "C:\some path\here"

Moving files from Subfolders into root directory, but not copying them to the next directory when ran again

I have a folder that contains subfolders with MP4 files. I'm trying to write a script that will move the MP4 files out of the subfolders into the root folder when ran. The batch file I wrote is working, but when the batch script runs again for new subfolders, the MP4 files that were already copied to the root folder, get moved up another level in the file structure. For example:
C:\MainRoot\Root\Subfolder\media.mp4
When script is ran, 'media.mp4' gets moved up to C:\Root\media.mp4 as desired.
But since I need the script to run on a scheduled task. The next time the script runs I get the following:
C:\MainRoot\media.mp4
Instead of just the MP4 file staying in C:\MainRoot\Root.
Here's my batch file so far to copy the mp4 files:
set root_folder=C:\MainRoot\Root
for /f "tokens=1* delims=" %%G in ('dir %root_folder% /b /o:-n /s ^| findstr /i ".mp4" ') do (
move /y "%%G" "%%~dpG..\%%~nxG"
)
What do I need to modify so that once moved, the MP4 files will stay in place?
Any help would be greatly appreciated!
Since all your source files seem to be at a certain directory level, a for /D loop could be wrapped around your for /F loop, which parses the output of a non-recursive dir command line (no /S):
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_ROOT=C:\MainRoot\Root"
set "_PATTERN=*.mp4"
rem // Loop through sub-directories:
for /D %%D in ("%_ROOT%\*") do (
rem // Loop through matching files:
for /F "eol=| delims=" %%F in ('dir /B "%%~fD\%_PATTERN%"') do (
rem // Avoid overwriting destination file:
if not exist "%_ROOT%\%%~nxF" (
rem // Move matching file one level up:
move /Y "%%~fD\%%~nxF" "%_ROOT%\%%~nxF"
)
)
)
endlocal
exit /B
If you are happy to overwrite as in your provided example then something as simple as this may suit your purpose:
#Echo Off
Set root_folder=C:\MainRoot\Root
If /I NOT "%CD%"=="%root_folder%" PushD "%root_folder%" 2>Nul||Exit/B
For /R %%G In (*.mp4) Do If /I NOT "%~dpG"=="%root_folder%\" Move "%%G">Nul 2>&1
If the files are only one folder deep you may prefer this:
#Echo Off
Set root_folder=C:\MainRoot\Root
If /I NOT "%CD%"=="%root_folder%" PushD "%root_folder%" 2>Nul||Exit/B
For /D %%G In (*) Do Move "%%G\*.mp4">Nul 2>&1

How to batch rename files in a directory from a list of names in a text file

I'd like to use Power Shell or a batch file to rename several files in a folder based on a list in a text file. Essentially I want to append the file names with the author's last names (which I have stored in a separate text file).
E.g. Currently I have:
C:\myfiles
9-ART-2013.pdf
4-EGO-2013.pdf
2-ART-2013.pdf
My text file (in same order as files):
C:\myfiles
_Smith
_Jenkins
_McMaster
I want the files to be renamed as follows:
9-ART-2013_Smith.pdf
4-EGO-2013_Jenkins.pdf
2-ART-2013_McMaster.pdf
I've seen similar problems where people want to recursively rename files but they are always using a generic common appending element like adding an underscore or pre-pending with folder name, etc.
e.g. https://serverfault.com/questions/6268/easy-way-to-rename-all-files-in-a-directory-in-windows
In PowerShell it would be:
$names = Get-Content c\myfiles
Get-ChildItem C:\somedir\*.pdf | Sort -desc |
Foreach {$i=0} {Rename-Item $_ ($_.basename + $names[$i++] + $_.extension) -WhatIf}
If it looks like it will copy correctly, remove the -WhatIf.
Any of the above are likely workable solutions with just a little tweaking but the simplest thing to do was to simply create one text file with each row containing "the current filename"... a TAB... then "the filename I wanted".
9-ART-2013.pdf 9-ART-2013_Smith.pdf
4-EGO-2013.pdf 4-EGO-2013_Jenkins.pdf
2-ART-2013.pdf 2-ART-2013_McMaster.pdf
Then save the file as rename_list.txt and create a batch file with the following code.
for /F "tokens=1,2" %%a in (rename_list.txt) do ren "%%a" "%%b"
pause
You can delete the pause line once you get it tweaked and running correctly. Just copy the rename_list.txt and batch files to the folder with the files you want to rename and run the batch file. If you have a large folder with many files names you can get them in a text file by running a batch file with the following line.
dir /D > filename_scrapper_output.txt
It will create a text file filename_scrapper_output.txt that you can use to start your rename_list.txt file for above.
Another way to achieve the same thing:
#echo off
setlocal EnableDelayedExpansion
rem Load the list of authors:
set i=0
for /F %%a in (myfiles.txt) do (
set /A i+=1
set "author[!i!]=%%a"
)
rem Do the rename:
set i=0
for /F %%a in ('dir /b *.pdf') do (
set /A i+=1
for %%i in (!i!) do ren "%%a" "%%~Na!author[%%i]!%%~Xa"
)
EDIT: New method added
If the list of names file have the "OLD-Name NEW-Name" structure, then the code is much simpler:
for /F "tokens=1,2" %%a in (myfiles.txt) do ren "%%a" "%%b"
Note that the names must be separated by a space.
#ECHO OFF &SETLOCAL ENABLEDELAYEDEXPANSION
<Text.txt (
for /f "tokens=1*delims=_" %%a in ('dir /b /a-d /o-d *-*-*.pdf') do if "%%b"=="" (
set "xand="
set /p "xand="
echo ren "%%~a" "%%~na!xand!%%~xa"
))
Taking Endoro's and simplifying:
#ECHO OFF &SETLOCAL ENABLEDELAYEDEXPANSION
<texts.txt (
for /f "tokens=* delims=* " %%a in ('dir /b /a-d /o-d *-*-*.pdf') do (
set "xand="
set /p "xand="
ren "%%~a" "%%~na!xand!%%~xa"
))

Batch script - files to folders - with addition

I'm working in a company that has far too many (millions) of record forms that all need organising.
Each file uses the following naming structure:
xxxxx-xx-xx-xxxxx e.g. 43144-02-40-21324.<ext>
I've used in the past a batch script that puts files into a folder of the same name, but I'm looking for something slightly different.
I'd eventually like to end up with the following folder structure:
C:\[root directory]\43144\02\40\21324.PDF
Is something like this possible?
My knowledge of batch scripts is non existant, does anyone have the ability to quickly throw this together?
Thanks in advance, David
#ECHO OFF
SETLOCAL
SET "sourcedir=."
SET "destdir=u:\temp"
FOR /f "tokens=1,2,3,*delims=-" %%a IN ('dir /b/a-d "%sourcedir%\*-*-*-*"') DO (
MD "%destdir%\%%a\%%b\%%c" 2>NUL
IF EXIST "%destdir%\%%a\%%b\%%c\%%d" (ECHO "%destdir%\%%a\%%b\%%c\%%d" already exists
) ELSE (
MOVE "%sourcedir%\%%a-%%b-%%c-%%d" "%destdir%\%%a\%%b\%%c\%%d" >nul
)
)
GOTO :EOF
This should set you on the right track - just need to set your source and destination directories...
Yes, this isn't very hard to do:
We need to loop over all the files
for %%F in (*.ext) ...
For simplicity reasons we look in the current folder, so set it appropriately beforehand. But you can also supply a folder to look in.
For every file found, just call a subroutine that does the work
... do call :process "%%~F"
Exit the main method
goto :eof
We need a subroutine now
rem :process <filename>
:process
Dissect the file name into its parts. Since those are all fixed-length we can just use substrings here.
set "Filename=%~1"
set "Part1=%Filename:~0,5%"
set "Part2=%Filename:~6,2%"
set "Part3=%Filename:~9,2%"
set "Rest=%Filename:~12%"
Now we should probably check whether the folder we need to copy this into already exists or not
if not exist C:\root\%Part1%\%Part2%\%Part3%\NUL mkdir C:\root\%Part1%\%Part2%\%Part3%
This will create every folder along the way if necessary. Very handy.
Rename and move the file now
move %1 C:\root\%Part1%\%Part2%\%Part3%\%Rest%
Exit the subroutine
goto :eof
This should be it, more or less (bugs in my implementation notwithstanding). So here again in full:
for %%F in (*.ext) do call :process "%%~F"
goto :eof
rem :process <filename>
:process
set "Filename=%~1"
set "Part1=%Filename:~0,5%"
set "Part2=%Filename:~6,2%"
set "Part3=%Filename:~9,2%"
set "Rest=%Filename:~12%"
if not exist C:\root\%Part1%\%Part2%\%Part3%\NUL mkdir C:\root\%Part1%\%Part2%\%Part3%
move %1 C:\root\%Part1%\%Part2%\%Part3%\%Rest%
goto :eof
You will have to iterate through files and for each file split name using FOR.
Then, you must ensure directories are created and copy/move your file to new path.
For %%f In (*.*) Do For /F "Tokens=1,2,3,4,5,6 Delims=-" %%i In ("%%f") Do Call :PutInPath %%f %%i %%j %%k %%k %%l
GoTo :EOF
:PutInPath
MD "C:\your dir\%2"
MD "C:\your dir\%2\%3"
MD "C:\your dir\%2\%3\%4"
REM You can use COPY, MOVE, REN, ...
COPY %1 "C:\your dir\%2\%3\%4\%5"
GoTo :EOF
#Echo off
SET extension=%1
setlocal enabledelayedexpansion
for %%f in ("*.%extension%") do (
SET substr=%%f
echo !substr!
copy !substr! !substr:~0,5!\!substr:~6,2!\!substr:~9,2!\!substr:~12,5!.%extension%
del !substr!
)
if the file format are fixed, we can do it like this!

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.

Resources