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.
Related
I have a root directory with over 25,000 files in it. These files are in loads of different subdirectories.
I also have a text file with 4300 lines in it, each line is an absolute path to one of the files in the directory. Like below,
c:\dir1\hat1.gif
c:\dir1\hat2.gif
c:\dir1\dir2\hat1.gif
c:\dir1\dir2\hat2.gif
c:\dir1\dir3\cat.zip
c:\dir1\dir3\banana.exe
I also have another root directory witch is a copy of the original root directory structure but all the directories are empty.
I would like to copy all the files listed in the text file to the directory which is empty and place all the copied files inn the respected subdirectories.
if I use the following batchfile I keep getting file overwrite prompts because it is not copying the files to the correct directories.
#echo off
set dst_folder=c:\DSTN2
for /f "tokens=*" %%i in (USEDFILES.txt) DO (
xcopy /S/E "%%i" "%dst_folder%"
)
How do I modify this so the files are copied to the correct directory?
Since you are copying specific files from a list, you need to make sure the directory structure exists in the destination if you want it in a similar folder structure. So using the power of the FOR command modifiers you can get the file path only from the file name in the file list. You will use that modifier to create the destination directory and also use it as the destination for the XCOPY command.
I have taken the liberty of providing best practices for all the code you are using.
#echo off
set "dst_folder=c:\DSTN2"
for /f "usebackq delims=" %%G in ("USEDFILES.txt") DO (
mkdir "%dst_folder%%%~pG" 2>NUL
xcopy "%%~G" "%dst_folder%%%~pG"
)
I had a bash xcopy script, but got a lot of issues. After a lot of searching and reading I guess robocopy was a better tool to do the copy.
(The script must run on windows 10 computers without installing anything else and without internet access)
I'm trying to make a bash script that copy (with robocopy) some local network folders and files to a local custom directory. The aim is to be able to access to the files off from the local network.
The path to folders and files are stored inside a txt file (each line = a path)
I want to keep the structure of folder I save locally.
For example the folder X:\Path\to some\local\network\folder\with\some & characters\ will result in C:\PathTolocalFolder\Path\to some\local\network\folder\with\some & characters\ (without the X:\ letter)
Based on many similar questions (but not all at the same time) I have done this :
#echo off
SETLOCAL EnableDelayedExpansion
cls
cd C:
chcp 28591 > nul
for /f "delims=*" %%a in ('type "X:\path with spaces & specials characters\List.txt"') do (
REM echo %%a
REM echo !%%a!
echo %%a to C:\PathTolocalFolder!%%a!
ROBOCOPY "%%~a" "C:\PathTolocalFolder!%%a!" /S /A+:RA /R:1 /W:5
)
It is partially a success, but :
As there are special characters everywhere in paths and files names, I got some issues. Specially with & characters. My double quotes doesn't solve the problem. How could I go better?
For some cases, I want to save some files but not the whole directory where they are. The full path to the file is inside the text file. But as robocopy needs to add a space between folder path and file filter I have do some manipulation. How can I detect and extract the file name when there is one to adapt the robocopy command?
I want to use an exclusion list like I was doing before with xcopy. But robocopy doesn't accept a file in input for exclusions. I tried this to extract the exclusion file:
for /f "usebackq tokens*" %%D in ("C:\path to exclusion file\exclusions.txt") do (
if NOT "!dirs!"=="" (
Set dirs=!dirs! "%%D"
else (
Set dirs ="%%D"
)
)
But doesn't really know what I am doing and how to combine with the first part.
Bonus questions I'm using the robocopy log file functionality (removed from below) is there a way to archive (by adding the date in the name for example) previous log file before creating the new one? Is it possible to remove the progress percents in the log file but to display it in the terminal instead? How to use the "/np" option for log file but not for terminal display?
It's hard to me to understand how the delayed variables are working in batch files and how the different methods to read a file or variable are working.
Any help is welcome :)
Sorry for my bad English skills
thank for having read
I want to take a file from a source directory (C:\Users\Desktop\Source\Test.xlsx) and want to replicate it to several folders, all having the same structure. So I have to change just one folder name. All done via one batch file and just one log-file which is created afterwards.
To have dynamically expansion of my batch-procedure here, I want to do a filtering of the elements of the root folder (C:\Users\Desktop\Replica\), which shows me the childs (one two three four five) where the file needs to be copied at inside a testfolder. There I have done the following at the moment, which is static:
set "list=one two three four five"
(
for %%i in (%list%) do (
xcopy "C:\Users\Desktop\Source\Test.xlsx" "C:\Users\Desktop\Replica\%%i\testfolder" /Y
echo(%%i
echo(
)
)>copylog.txt
So, my question is basically who I do the listing of just the folders under "replica" and put those inside a list variable I can use in the code as already written. I only found sources where these are just saved within a separate .txt-file, but I just want to keep it inside the batch.
Any guesses on this one?
By the way: I experienced by saving a path just as a variable, I couldn't execute xcopy %source% %target% (even when having "%source%" or source = "Path"). What is the problem here?
You don't need to search the directories, put them into a variable and then iterate over the values in the variable. Just iterate over the directories
...
for /d %%i in ("C:\Users\Desktop\Replica\*") do (
xcopy /y "C:\Users\Desktop\Source\Test.xlsx" "%%~fi\testfolder"
...
The for /d will iterate over the indicated set of directories. In the xcopy command %%~fi is used to retrieve the full path of the directory for the current iteration.
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"
I'm totally unfamiliar with batch files but I'm pretty sure I need one for the task at hand:
I want to run pdfcrop for all the files in a particular directory and store the cropped files in a new directory. New directory is called 'croppedfiles' and if it doesn't already exist in the location where the pdfs are stored then such a directory is created and the output files are stored there.
I'd like the output files to have the same name as old files with the addition of '_cpp' at the end.
syntax for pdfcrop is just pdfcrop input.pdf output.pdf
Referring to the poor material you provided for this question - I mean only your for cycle quotation - I can only suggest a pair of tips: first you can narrow the group of files on which the for cycle is going to work, using a piped find ".pdf" command. Then you can use another nested for cycle to obtain the name of the file to be processed by pdfcrop and to set it as a variable to be used for the output path. Here is the example script:
for /f "delims=" %%g in ('dir ^"[set your desired path]^" ^| find
^".pdf^"') do ( set VAR=%%g for /f "tokens=1,2 delims=." %%m in
('!VAR!') do (mkdir "[chose your subdirectory name]" pdfcrop "!VAR!" "[subdirectory]\%%m[set
your additional characters].%%n" ) )
I hope it works, because I have been not able to test it.