I'm learning about CMD for loops. I created a directory tree rooting in the folder C:\Users\Ahmed\Desktop\Playing_Field:
├───New folder - Copy
├───New folder - Copy (10)
├───New folder - Copy (11)
├───New folder - Copy (12)
├───New folder - Copy (13)
├───New folder - Copy (14)
├───New folder - Copy (15)
├───New folder - Copy (16)
├───New folder - Copy (17)
├───New folder - Copy (18)
├───New folder - Copy (19)
├───New folder - Copy (2)
├───New folder - Copy (20)
├───New folder - Copy (21)
├───New folder - Copy (22)
├───New folder - Copy (23)
├───New folder - Copy (24)
├───New folder - Copy (25)
├───New folder - Copy (26)
├───New folder - Copy (27)
├───New folder - Copy (28)
├───New folder - Copy (29)
├───New folder - Copy (3)
├───New folder - Copy (30)
├───New folder - Copy (31)
├───New folder - Copy (32)
├───New folder - Copy (33)
├───New folder - Copy (34)
├───New folder - Copy (35)
├───New folder - Copy (36)
├───New folder - Copy (4)
├───New folder - Copy (5)
├───New folder - Copy (6)
├───New folder - Copy (7)
├───New folder - Copy (8)
└───New folder - Copy (9)
Then I tried to filter through them using this command:
for /d %n in (C:\Users\Ahmed\Desktop\Playing_Field\*3*) do #echo "%n"
The output was:
"C:\Users\Ahmed\Desktop\Playing_Field\New folder - Copy (10)"
"C:\Users\Ahmed\Desktop\Playing_Field\New folder - Copy (20)"
"C:\Users\Ahmed\Desktop\Playing_Field\New folder - Copy (16)"
"C:\Users\Ahmed\Desktop\Playing_Field\New folder - Copy (13)"
"C:\Users\Ahmed\Desktop\Playing_Field\New folder - Copy (23)"
"C:\Users\Ahmed\Desktop\Playing_Field\New folder - Copy (3)"
"C:\Users\Ahmed\Desktop\Playing_Field\New folder - Copy (30)"
"C:\Users\Ahmed\Desktop\Playing_Field\New folder - Copy (31)"
"C:\Users\Ahmed\Desktop\Playing_Field\New folder - Copy (32)"
"C:\Users\Ahmed\Desktop\Playing_Field\New folder - Copy (33)"
"C:\Users\Ahmed\Desktop\Playing_Field\New folder - Copy (34)"
"C:\Users\Ahmed\Desktop\Playing_Field\New folder - Copy (35)"
"C:\Users\Ahmed\Desktop\Playing_Field\New folder - Copy (36)"
"C:\Users\Ahmed\Desktop\Playing_Field\New folder - Copy (2)"
Why are there some folders in the output that do not contain 3 in their names, such as New folder - Copy (2)?
Modern Windows systems still support so-called short file names and even may have them enabled by default, besides the usual long files names. These short file names, also called 8.3 names (because of up to 8 characters for the base name and up to 3 characters for the extension), originate from MS-DOS and pre-Windows 95 systems, and are automatically generated in the background1.
To display such short file names, use the dir command, together with its /X option:
>>> dir /X /-C /A:D-H-S "%SystemDrive%\Progr*"
Volume in drive C has no label.
Volume Serial Number is 0000-0000
2022/07/10 19:00 <DIR> PROGRA~1 Program Files
2022/07/10 19:00 <DIR> PROGRA~2 Program Files (x86)
0 File(s) 0 bytes
2 Dir(s) 412316860416 bytes free
Now wildcards (like * and ?) match against both the long and the short file names:
>>> dir /X /-C /A:D-H-S "%SystemDrive%\Pro*1"
Volume in drive C has no label.
Volume Serial Number is 0000-0000
2022/07/10 19:00 <DIR> PROGRA~1 Program Files
0 File(s) 0 bytes
1 Dir(s) 412316860416 bytes free
To always match against the long file names, use find or findstr to post-filter the returned items by dir /B:
>>> dir /B /A:D-H-S "%SystemDrive%\Progr*" | find /I "Program"
Program Files
>>> dir /B /A:D-H-S "%SystemDrive%\Progr*" | findstr /I /B "Program"
Program Files
Or, applied to your code, using for /F to capture the result:
>>> for /F "eol=| delims=" %I in ('dir /B /A:D-H-S /O:N "%UserProfile%\Desktop\Playing_Field\*3*" ^| find /I "3"') do #echo "%I"
"C:\Users\Ahmed\Desktop\Playing_Field\New folder - Copy (13)"
"C:\Users\Ahmed\Desktop\Playing_Field\New folder - Copy (23)"
"C:\Users\Ahmed\Desktop\Playing_Field\New folder - Copy (3)"
"C:\Users\Ahmed\Desktop\Playing_Field\New folder - Copy (30)"
"C:\Users\Ahmed\Desktop\Playing_Field\New folder - Copy (31)"
...
>>> for /F "eol=| delims=" %I in ('dir /B /A:D-H-S /O:N "%UserProfile%\Desktop\Playing_Field\*3*" ^| findstr /I "3"') do #echo "%I"
"C:\Users\Ahmed\Desktop\Playing_Field\New folder - Copy (13)"
"C:\Users\Ahmed\Desktop\Playing_Field\New folder - Copy (23)"
"C:\Users\Ahmed\Desktop\Playing_Field\New folder - Copy (3)"
"C:\Users\Ahmed\Desktop\Playing_Field\New folder - Copy (30)"
"C:\Users\Ahmed\Desktop\Playing_Field\New folder - Copy (31)"
...
1) Note that disabling the short file names will not remove already generated such names.
Related
I am trying to exclude copying the files from the path :
AlreadyCopied.txt.
AlreadyCopied.txt looks like this:
file 'E:\teste-batch-file\original\CG Animation Of Outerspace - Copy - Copy - Copy (1) - Copy.mp4'
file 'E:\teste-batch-file\original\CG Animation Of Outerspace - Copy - Copy - Copy (2) - Copy.mp4'
file 'E:\teste-batch-file\original\CG Animation Of Outerspace - Copy - Copy - Copy (3) - Copy.mp4'
file 'E:\teste-batch-file\original\CG Animation Of Outerspace - Copy - Copy - Copy (4) - Copy.mp4'
I used xcopy
xcopy "%Source%\*.mp4" "%Target%" /EXCLUDE:AlreadyCopied.txt
I also tried with RoboCopy:
RoboCopy "%Source%\*.mp4" "%Target%" /XF "AlreadyCopied.txt" /Copy
Both with no luck.
Thank you for help.
On a server i have a main folder that contain many subfolder and file with some extension.
I would like to compress only file with extension *.bak and each file zip created in right subfolder path.
Example:
- Folder
-- Subfolder1
--- File.zip
--- File.bak
-- Subfolder2
--- File2.zip
--- File2.bak
Now I use this script to compress:
cd /Folder/Subfolder1
set extension=.bak
for %%a in (*%extension%) do 7za.exe a "%%~na.zip" "%%a"
cd /Backup/Subfolder2
set extension=.bak
for %%a in (*%extension%) do 7za.exe a "%%~na.zip" "%%a"
This script is right, but Ihave to remember to create new lines everytime that I have a new subfolder.
I there a solution that check every subfolder for *.bak files and compress it in each subfolder?
Thankyou very much and sorry for bad english.
Addendum:
I tried this command:
set extension=.bak
for **/R** %%a in (*%extension%) do 7za.exe a "%%~na.zip" "%%a"
this search correctly every *.bak in every subfolder, create a single zip for every bak that find BUT it create the n. zip file all in main folder...
I want to copy multiple folders with the same ending from a main folder to another folder. My .bat file doesn't work unfortunately. This batch file will be put in the same directory as the main folder.
Folder structure:
Folderdirectory/main folder/folderxxx10.1, folderyyy10.2, folderzzz10.3, etc.
Location of the batch file:
Folderdirectory/copy.bat
Where to copy the folders:
Folderdirectory/1 AAA
1 AAA is the destination folder (The space is on purpose). Other destination folders have the same structure: 2 BBB , 3 CCC, etc.
For example: All folders in Folderdirectory/main folder/ with the ending "10.1" should be copied to an existing folder Folderdirectory/1 AAA.
This should be looped in order to copy folders with the ending 10.2 (copy to Folderdirectory/2 BBB ), 10.3 (copy to Folderdirectory/3 CCC ) , ..., 10.30
The code I wrote:
for /L %%i in (1,1,30) DO (
xcopy "%~dp0%\main folder\*10.%%i" "%~dp0\%%i *" /E
)
PAUSE
Error I get: (example for i=30)
File not found - 30 *
0 File(s) copied
Thank you in advance! I am new to coding and would be very grateful for any help.
Kind regards,
TamTam
UPDATE: I updated the code:
for /L %%i in (1,1,30) do for /D %%j in ("%~dp0\main folder*10.%%i") do
xcopy /E /I "%%~j" "%~dp0\%%i*")
PAUSE
The batch file now copies all folderxxx10.1, folderyyy10.2, etc. to Folderdirectory/ and not to Folderdirectory/1 AAA, Folderdirectory/2 BBB for instance. So, there must be something wrong with the destination "%~dp0\%%i*".
Furthermore, it copies the files in the folder, not the folder itself.
Thanks so far for your help!
Let's say I'm in C:\test directory, where I have C:\test\myHugeFolder directory and a C:\test\backup directory - and I'd like to copy myHugeFolder into backup from cmd.exe Command Prompt.
So, I thought this usage of xcopy is correct, by using relative paths:
C:\test> xcopy myHugeFolder backup\ /s /e
The thing is, xcopy here was churning for like 15 minutes, also listing each file in myHugeFolder, so I thought all was fine - then when it finished, I look into backup, and there no myHugeFolder; in fact when I search for myHugeFolder, there's only the original:
C:\test>dir myHugeFolder* /s
Volume in drive C has no label.
Volume Serial Number is FFFF-FFFF
Directory of C:\test
18-10-2015 16:26 <DIR> myHugeFolder
0 File(s) 0 bytes
Total Files Listed:
0 File(s) 0 bytes
1 Dir(s) 2.419.708.346.368 bytes free
So, obviously that is not the right command line - where am I going wrong, and what is the right invocation of xcopy to do this kind of a copy?
The test in the question is wrong.
C:\test> dir myHugeFolder
This command will not list anything copied to the C:\test\backup folder.
A correct test is more like this:
C:\test> dir backup
It would show that the contents of C:\test\myHugeFolder was copied into C:\backup, not C:\test\backup\myHugeFolder.
If one wanted a duplicate of C:\test\myHugeFolder in C:\test\backup\myHugeFolder, one way to do that would be:
C:\test> XCOPY myHugeFolder backup\myHugeFolder /E /I
After which the following command would show the desired copy of the myHugeFolder container:
C:\test dir backup\myHugeFolder
This is my copy command :
set POLL="C:\Documents and Settings\"
copy Register.class+Ca.class+pack %POLL% /-Y
pack is a folder here.
The result of the above copy coman is that only Register.class gets copied to the destination folder. What's my mistake?
Naming multiple source files for a copy command copies the files into one single file.
copy fileone + filetwo filethree
would result in filethree containing the content of fileone AND filetwo. You cannot copy multiple files to a different location with copy.
However it is fairly easy to do so using either a loop or xcopy-command:
set POLL="C:\Documents and Settings\"
FOR %%F IN (Register.class Ca.class) DO copy %%F %POLL% /-Y
xcopy pack %POLL% /-Y