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
Related
I have a list of filenames without their extension and a folder and sub-folders of those files with their extensions. I am trying to use the list to copy those files to a different location. I tried to use a script I found here and modified it:
#echo off
FOR /R "P:\Case\MyCase\Productions" %%G in (.) do (
for /F "delims=" %%i in (UniqueFileList.txt) do (
if exist %%G\%%i.* xcopy %%G\%%i.* "C:\Temp\CopiedFiles" /D /Y
)
)
My file names are TIN00001.msg, TIN00002.txt, TIN00003.jpg, etc...
and the names in my file list is TIN00001, TIN00002, TIN00003, etc...
How can I use a script to copy the files ?
Any help is appreciated.
Thanks !!
Mustu
I'm not sure if I fully understand your intention but my best guess is that something like this would suit you.
#Echo Off
Set "rootDir=P:\Case\MyCase\Productions"
Set "destDir=C:\Temp\CopiedFiles"
Set "listTxt=%~dp0UniqueFileList.txt"
For /F "Delims=" %%A In ('Where/R "%rootDir%" *.*^|FindStr/LIG:"%listTxt%"'
) Do FindStr/LIX "%%~nA" "%listTxt%">Nul 2>&1 && XCopy "%%A" "%dstDir%" /D /Y
If %listTxt% isn't in the same location as the script then change it's location to a full path too.
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..
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.
I would like to rename all the .log as .ok from a particular folder and subdirectories
The following will usually work just fine:
#echo off
for /r "PathToYourFolderHere" %%F in (.) do ren "%%F\*.log" *.ok
But the above can have problems if short file names are enabled on your drive and you have extensions longer than 3 characters. It will also rename files like name.log2 because the short name will have an extension of .log.
The following will only rename true .log files:
#echo off
for /f "eol=: delims=" %%F in (
'"dir /b /s /a-d PathToYourFolder\*.log|findstr /lie .log"'
) do ren "%%F" *.ok
Note: The rules for how RENAME treats wildcards can be found at How does the Windows RENAME command interpret wildcards?
run a .bat file from the folder containing:
for /R %%x in (*.log) do rename "%%x" "%%~nx.ok"
/R for recursive
%%~nx for the filename without extension
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