How to copy only files(not directories) using batch file? - batch-file

The directory structure is:
Images
-Folder1
-image1.jpg
-image2.jpg
-Folder2
-image3.jpg
-image4.png
-Folder3
-image6.png
-image7.jpg
-Folder4
I want to copy all images(i.e *.jpg, *.png) files only (not the folders) into the parent directory("Images").
I have tried using "robocopy" as follows:
robocopy /np ..\..\Exam\Images ..\..\Exam\Images *.jpg *.png /S
Here all files as well as folders are copied :(. But I need only files to be copied. How to do this?
Many many thanks in advance!

Try this on the command line:
for /r "Images" %i in (*.jpg *.png) do copy "%~fi" "my\target folder"
For a bach script the % must be doubled %%.

I think COPY or XCOPY is best used for files while I prefer Robocopy when dealing with folders.
Using the posted example try: (adjust paths to suit your needs.
#Echo off
For /f %%b In ('Dir C:\Exam\Images /b /s /a:d') Do (
Robocopy %%b C:\Exam\Images *.jpg *.png /xx /np
)

an easier way of doing this might be
for /r %%p in (*.png, *.jpg) do copy %%p destinationFolder.

Robocopy only has the /XD switch to exclude directories but it excludes the whole directory. I'd use a batch file to do it instead.
Try this:
#echo off
setlocal
for /f %%a in ('dir *.jpg *.png /b /s /a-d') do (
copy %%a PathToImagesFolder
)

there is an easy to use program called Drop-It if this is a repetitive task then you can use this to sort|move|copy the files to a single directory. hope this helps

Related

Delete everything from Subfolders through Batch file

I have directories like:
c:\Project\Current\stage1\somefiles and some folders
c:\Project\Current\stage2\somefiles and some folders
c:\Project\Current\stage3\somefiles and some folders
c:\Project\Current\stage4\somefiles and some folders
c:\Project\Current\stage5\somefiles and some folders
.
.
.
c:\Project\Current\stage500\somefiles and some folders
I want to create a batch file so that everything inside stage1, stage2,..., stage500 will get deleted but not any of other folders so that I can still see the above directories but empty.
Can someone please help?
Try this:
#echo off
CD c:\Project\Current /d
for /f "tokens=*" %%f in ('dir /a-d /s /b') do (
del "%%f" /q /f
)
There are three important parts:
for /f "tokens=*" %%f means we are iterating over all lines that are generated by the following command and temporarily save each line in the variable %%f for each iteration.
dir /a-d /s /b is the core of the code. This will list all files inside c:\Project\Current\ including all subfolders. /a-d means that directories will be ignored as we don't want them to be erased. /s means we are searching any subfolder. /b sets the output format to simple mode so that each line of the output will contain nothing but the full path to a file.
del "%%f" /q /f simply deletes the file which is stored in %%f. /q means "don't ask me if I'm sure, just erase it" and /f means that any file - even if it is marked as system file or as invisible or protected - will be deleted. Don't miss the quotation marks around %%f as otherwise paths containing spaces will cause trouble.
I found the answer and is very simple
for /d %%X in (c:\Project\Current*) Do (
for /D %%I in ("%%X\*") do rmdir /s/q "%%I"
del /F /q "%%X\*")
Thanks for everyone's help..

Recreate directory structure and copy newest file

I have 100+ sub-directories all under the same folder that I'm looking to copy the newest file to backup location with the directory structure intact.
\data\sub1\newest.file -> \backup\sub1\newest.file
\data\sub1\older.file1.ignore
\data\sub1\older.file2.ignore
\data\sub2\newest.file -> \backup\sub2\newest.file
\data\sub2\older.file1.ignore
etc....
Here's what I have so far, and i can't seem to piece it together. Any help would be greatly appreciated.
#echo off
set source="c:\data"
set dest="n:\backup"
if not exist %dest% md %dest%
cd /d %source%
for /d %%x in ("%source%"/*.*) do (
if not exist "%dest%\%%x" md "%dest%\%%x"
FOR /F %%I IN ('DIR *.* /A-D /B /O-D') DO COPY %%I "%DEST%\%%X" & #ECHO %%I COPIED TO "%DEST%\%%X"
)
I would try to do this with robocopy if I were you, because this will most likely be a more robust solution.
Windows' built-in xcopy program provides a flag to include empty directories.
C:\>xcopy "%source%" "%dest%" /E
But, it sounds like you may want to only copy newer/missing files. If that is the case, then #Marged has it right. You should use robocopy.
C:\>robocopy "%source%" "%dest%" /E
Check out robocopy /? for all the details and additional commands.
The metavariable %%x in your for statement is CaSe-SeNsItIvE so you must use %%x throughout the loop, but you are using %%X in the copy statement.
Since you only want to copy the first file, you should append &goto alabelotsideoftheforloop which terminates the for..x.. after the first file has been copied.

Copy only htm not html files batch windows

I created a windows batch file to copy only files with specific extensions into a different folder. Here is the line of code I used:
for /R "%cd%" %%f in (*.htm) do copy "%%f" "%cd%\myfolder"
The issue is that this will copy any extension that starts with .htm, i.e. .html, which I do not want; only .htm. How is this copy prevented?
I've tried
"(*.htm)"
("*.htm")
(*".htm")
(*."htm")
(*.htm*)
Thanks
Solution:
for /R %%f in (*.htm) do if /I "%%~xf" == ".htm" copy "%%f" "myfolder"
Thanks #Aacini and #Monacraft
This should work:
for /R %%f in (*.htm) do if /I "%%~xf" == ".htm" copy "%%f" "myfolder"
A couple comments unrelated to your problem:
%cd% is a variable that is always replaced by the current folder. If you give any name without a previous path, the name is assumed to be in the current folder. This way name and %cd%\name is exactly the same and the second one is customarily never used.
In for /R [path] %%f ... command, if the path is not given, the current folder is assumed.
You could check using an if statement:
for /R "%cd%" %%f in (*.*) do if /i "%%~xf"==".htm" copy "%%f" "%cd%\myfolder"
And that is the logical way to do this in batch.
Mona
The reason *.htm matches .html files is because of short 8.3 file names. A file with .html extension will have a short name with .htm extension.
Monacraft and Aacini have provided working solutions using IF statemnts within the body of the loop.
Here is a solution that uses DIR /B piped to FINDSTR within a FOR /F IN() clause.
for /f "eol=: delims=" %%F in ('dir /b /s /a-d-h-s *.html ^| findstr /lie .htm') do copy "%%F" "myfolder"
There's probably a better way, but (*.ht?) should do it.
If you are working with a windows vista or later OS, you can use robocopy and exclude not needed files
robocopy "%cd%" "%cd%\myFolder" *.htm /xf *.html

batch file picking up files from directory and subdirectories

I have a batch file that is appending a date to all to the file name of all CSV files. I only want CSV files in one directory to be picked up and no subdirectories. It appears to be running through all subdirectories though.
I have this code currently in the batch file
:: copy files
For /f "delims=" %%a in ('Dir /A:-D /b /s "%LOCALDIR%"*.csv 2^>nul') do If exist "%%a" (
COPY "%%a" "%LOCALDIR%%dtt%-%%~na.csv"
DEL "%%a"
)
I have tried getting rid of the /s in the code but then no files are picked up in the directory I want to look for.
Any help is greatly appreciated.
Why not just use a simple loop like?
pushd %LOCALDIR%
for %%A in (*.csv) do ren "%%~A" "%dtt%-%%~A"
popd
Or for a one liner
for %%A in (%LOCALDIR%\*.csv) do ren "%%~A" "%dtt%-%%~A"
If the path has spaces in it remember to use the usebackq option.

cleanup batch file

I'm writing a batch file to cleanup my source folders. I want to delete all wincvs generated files with a prefix of .#
FOR /F "tokens=*" %%G IN ('DIR /B .#*.*') DO DEL "%%G"
the problem I'm having is that it's not deleting my files within subdirectories.
I think you need DEL /S
you probably want to do
DIR /S /B .#*.*
to list out the directories recursively
What about this:
FOR /R C:\FOLDER\SUBFOLDER %%G IN (.#*.*) DO DEL %%G

Resources