Move Batch files to folder with similar name - windows

I am trying to move batch files to their respective folder, they have similar names expect the batch files I want to move have "Build" on the end. For example:
Folder name: wld-vine-tstrm
Bat file name: wld-vine-tstrmBuild.bat
In the same parent directory, I also have batch files with identical names to the folder, I do not want these to move, only the ones with "Build" at the end of the file names.
setlocal enabledelayedexpansion
set folderpath=E:\Build\ModelBatches\world-wild\actor50
for %%f in (%folderpath%\*.bat) do (
set "foldername=%%~nf"
move "%%f" "!foldername:~0,-5!"
)
This is very close to the result I am looking for; it moves the "Build" batches to the respective folder, but it also removes the last 5 characters from the batches I dont want to move.
The below batch files are now missing the last 5 chars, "tstrm"
To summarize, There are two batch files for each folder in a directory, I only want to move the one ending "Build" to its folder.

You should use this:
setlocal enabledelayedexpansion
set folderpath=E:\Build\ModelBatches\world-wild\actor50
for %%f in (%folderpath%\*build.bat) do (
set "foldername=%%~nf"
move "%%f" "!foldername:~0,-5!"
)
The wildcard *build.bat finds all batch files ending with "build" but wildcard "*.bat" shows them all.

Related

Run batch script on all files in a folder

I have a folder receiving 'calculations' files daily. Sometimes a few files arrive. I want to execute a batch script on all of them in a loop. Files may have different name but may contain three extensions only: .ta, .tb, .tc.
I am trying to run a batch script on all of them, rename the files so that they contain a timestamp and move to archive folder. I have tried multiple combinations of the below but I can't figure it out:
cd C:\test\files
FOR %%x IN (*.ta, *.tb, *.tca) DO (
call C:\test\batches\combine.bat
move %%x "C:\test\archive\calculations.txt-%date:/=-%_%time::=-%"
)
In the above case, combine.bat is not executed properly and the file is not moved. However, when I run it as follows it is launching combine.bat and moving the file. But if I have two files, combine.bat is executed on both but the second one is not moved to archive folder:
FOR /R C:\test\files %%x IN (*.ta, *.tb, *.tca) DO (
call C:\test\batches\combine.bat
move %%x "C:\test\archive\calculations.txt-%date:/=-%_%time::=-%"
)
The problem is that there are other folders under C:\test\files and I want the 'combine.bat' to run for this specific path only and not for any sub-folders. Is it failing because I have changed directory in the beginning to C:\test\files and the loop is then calling to another directory?
Appreciate you help.

Batch script - moving and renaming files in one step

I have to write a batch script to school, which moves all .jpg files from one folder to other one and rename them at the same time. I have a problem with for loop.
set i=1
for /R %folder1% %%F in (*.jpg) do (
move "%folder1%\%%F" "%folder2%\%newname%_%i%.jpg
set /a i+=1 )
Newname is a variable entered by user.
For example if I have files named file09.jpg, fvfdfv.jpg, whatever.jpg I need to rename them to %newname%_1.jpg, %newname%_2.jpg, %newname%_3.jpg.
My problem is, that it moves all files from folder1, but in folder2 there is just one file named newname_1.jpg ... Like if all files was moved to a single one..
Do you have any tips please?

Need batch program that moves files without copying then deleting them

Every month I have a very large number of files (in many subfolders) in a particular folder. I need to move them all into a different folder. In an attempt to automate the process of moving them I used robocopy in a batch file. It works fine, but takes HOURS to run. (It is many many GB).
Now, if I do it manually in Windows Explorer, by opening said folder, selecting all, and right-dragging to destination folder, and choosing "Move Here", it moves INSTANTLY. (Windows XP must be pruning and grafting the directory entries, without ever making a second copy of the files. ... and yes, source and destination are on same partition.)
So, QUESTION IS: Does anyone know of a program I can run from a batch file to move files in this instantaneous way? (need to move entire sub-folder tree)
You can use MOVE for this:
C:\>MOVE /?
Moves files and renames files and directories.
To move one or more files:
MOVE [/Y | /-Y] [drive:][path]filename1[,...] destination
To rename a directory:
MOVE [/Y | /-Y] [drive:][path]dirname1 dirname2
[drive:][path]filename1 Specifies the location and name of the file
or files you want to move.
destination Specifies the new location of the file. Destination
can consist of a drive letter and colon, a
directory name, or a combination. If you are moving
only one file, you can also include a filename if
you want to rename the file when you move it.
[drive:][path]dirname1 Specifies the directory you want to rename.
dirname2 Specifies the new name of the directory.
/Y Suppresses prompting to confirm you want to
overwrite an existing destination file.
/-Y Causes prompting to confirm you want to overwrite
an existing destination file.
The switch /Y may be present in the COPYCMD environment variable.
This may be overridden with /-Y on the command line. Default is
to prompt on overwrites unless MOVE command is being executed from
within a batch script.
For example:
C:\Users\test>mkdir to
C:\Users\test>move from\*.txt to
C:\Users\test\from\1.txt
C:\Users\test\from\2.txt
2 file(s) moved.
With a point in the right direction from #psmears, and a lot of googling, I found the (or a) solution:
#echo off
setlocal ENABLEDELAYEDEXPANSION
REM ** Change to source dir
L:
cd "L:\Backups\Source"
REM ** Move files recursively
for /R %%G in (.) do (
set mydir=%%G
set mynewdir=!mydir:~18!
md "L:\DEST\!mynewdir!"
cd "L:\DEST\!mynewdir!"
move "%%G\*.*" .
)
REM ** Remove now-empty sub-dir structure inside source
L:
cd "L:\Backups\Source"
for /f "usebackq delims=" %%d in (`"dir /ad/b/s | sort /R"`) do rd "%%d"

Need a BAT file to copy & rename all files in specific tree

I need to create a .bat that runs through a multilayered directory... copying certain files that contain the following suffix: '.full.jpg' to save as '.jpg'
What I've tried:
copy /y "C:\Users\myname\Desktop\maindir\*.full.jpg" "C:\Users\myname\Desktop\maindir\*.jpg"
However, I cannot get it to work.
The .bat is located in the 'maindir' directory and ran from the terminal (cmd).
Here's an example scenario that maps closely to mine:
Existing Files:
C:\Users\myname\Desktop\maindir\a\a\picture1.full.jpg
C:\Users\myname\Desktop\maindir\a\a\picture3.full.jpg
C:\Users\myname\Desktop\maindir\a\b\picturea.full.jpg
C:\Users\myname\Desktop\maindir\a\b\pic1.full.jpg
C:\Users\myname\Desktop\maindir\b\a\foto.full.jpg
C:\Users\myname\Desktop\maindir\b\a\photo.full.jpg
C:\Users\myname\Desktop\maindir\b\b\pic1.full.jpg
C:\Users\myname\Desktop\maindir\b\c\pi2.full.jpg
Example Output Wanted:
C:\Users\myname\Desktop\maindir\a\a\picture1.full.jpg
C:\Users\myname\Desktop\maindir\a\a\picture1.jpg
C:\Users\myname\Desktop\maindir\a\a\picture3.full.jpg
C:\Users\myname\Desktop\maindir\a\a\picture3.jpg
C:\Users\myname\Desktop\maindir\a\b\picturea.full.jpg
C:\Users\myname\Desktop\maindir\a\b\picturea.jpg
C:\Users\myname\Desktop\maindir\a\b\pic1.full.jpg
C:\Users\myname\Desktop\maindir\a\b\pic1.jpg
C:\Users\myname\Desktop\maindir\b\a\foto.full.jpg
C:\Users\myname\Desktop\maindir\b\a\foto.jpg
C:\Users\myname\Desktop\maindir\b\a\photo.full.jpg
C:\Users\myname\Desktop\maindir\b\a\photo.jpg
C:\Users\myname\Desktop\maindir\b\b\pic1.full.jpg
C:\Users\myname\Desktop\maindir\b\b\pic1.jpg
C:\Users\myname\Desktop\maindir\b\c\pi2.full.jpg
C:\Users\myname\Desktop\maindir\b\c\pi2.jpg
I'd appreciate any help towards this as I haven't been able to do it yet. I will run across a directory structure whereby the top level directory will contain 15+ directories and each containing 20+ directories with 100+ files in each lowest directory.
Thanks.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=u:\Users\myname\Desktop\maindir"
FOR /r "%sourcedir%" %%a IN (*.full.jpg) DO (
FOR %%b IN ("%%~dpna") DO ECHO(COPY "%%a" "%%~dpnb.jpg"
)
GOTO :EOF
The inner for examines the drive-path-name only of the complete filename in %%a (ie. it drops the .jpg) and delivers the drive-path-name of that name (ie. drops the .full) to which you add .jpg and job done.
You would need to change the setting of sourcedir to suit your circumstances.
The required COPY commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(COPY to COPY to actually copy the files.

Change folder name if it contains file type

I am wanting to search a set of folders in a given filepath for a specific file type like so and rename the parent folder
if exist %filepath%**.bz2 rename parent folder
The problem is, I don't know what the parent folder is called every time I run the script and is subject to change. Any ideas?
The following script will list all folders within a directory tree that contain at least one *.bz2 file:
#echo off
setlocal
set "root=d:\test"
for /r "%root%" %%A in (.) do if exist "%%A\*.bz2" echo %%~fA
You didn't state how you want to rename the folders, so you will have to figure out how to modify the above to do the rename.

Resources