Paths with spaces and wildcards - cmd

I have this command line for loop
for /d %f in ("C:\Documents and Settings\Moi\Desktop\New Folder\*") do rmdir %f
It appears to be correct but it breaks up the path anyways even with the double quotes. I've tried using the hex equivalent (0x22) and that did not help either. Is there another way to handle paths with spaces in them?
If I simply try
for /d %f in ("C:\Documents and Settings\Moi\Desktop\New Folder\New Folder") do rmdir %f
It keeps the whole string but as soon as I add the wildcard it breaks the path up. The overall goal is to delete all the folders in this folder without deleting the folder itself.
Note:These are just test folders while trying to solve this problem, the real folders could be named anything.

I usually solve this problem by using the old DOS 8 character path
"Documents and settings" is: "docume~1"
"Program files" is "progra~1"
"new folder" is newfol~1
If you have multiple similar names in a folder then the ~1 at the end is a counter
new folder = newfol~1
new folder2 = newfol~2
new folderhuppla = newfol~3

Double quote the rmdir argument. Also double the % when used in a batch file.
for /d %f in ("C:\Documents and Settings\Moi\Desktop\New Folder\*") do rmdir "%f"

Related

CMD DIR filename and path

I perform a search in the CMD shell.
The goal is to find all files with an x from DRIVE C. I only need to see the path and the filename.
I have tried this so far. If I insert the following, I only get a path.
DIR "C:\*x*.*" /s /b
How can I also return the filename?
You could use the Where command too:
Where/R C:\ *x*
You can add an /F too which places all output files into double quotes.
Where/F /R C:\ *x*
Drives require terminating with a back slash as above directories do not.
Where/F /R C:\Users\belsober *x*
Note: I've maintained the search mask you used, just remember that this will also match every single file extension too, e.g. .xml, .xls, .txt, .ocx, .exe and .docx

Can use commands at cmd prompt but can't create a *.bat file with it

I was successfully searching the web for a way to create a new folder containing ALL the .otf and .ttf files located in a different folder with subdirectories (one folder for each font, a total of 24 subfolders).
I used this:
for /r %x in (*.ttf, *.otf) do copy "%x" G:\dropbox\9mediendesignfachfrau\fonts\allezam\ /y
When I open cmd,
C:...>G:
G:...>cd \dropbox\9mediendesignfachfrau\fonts
G:...>for /r %x in (*.ttf, *.otf) do copy "%x" G:\dropbox\9mediendesignfachfrau\fonts\allezam\ /y
and enter, everything works fine.
I tried pasting that into a .bat file located in the \fonts\ folder and tried starting it. cmd popped up and closed immediately, nothing else happened.
Could you help me finding my mistake?
It's still less work entering every line in cmd, press enter, enter the next line and so on than installing every single font manually but I'm quite lazy so I prefer a single double-click on a .bat file :P
When you use it in a batch file, the percent sign must be double,so,%x must be changed to %%x and your code looks like this :
#echo off
for /r %%x in (*.ttf, *.otf) do copy "%%x" G:\dropbox\9mediendesignfachfrau\fonts\allezam\ /y
pause

Batch Script command at prompt

This may seem like a stupid question but I have this Simple Script:
#ECHO OFF
XCOPY c:\test c:\backupfolder /m /e
Because there is no folder backupfolder it prompts you if it is a folder or file how using the script to automatically put in The Letter "D" for directory in the prompt
Thanks
When copying to a folder, which is the usual case, add a backslash to the end of the path.
Adding quotes protects the command from spaces etc in the paths.
#ECHO OFF
XCOPY "c:\test\*.*" "c:\backupfolder\" /m /e
The /i switch causes xcopy to assume the destination is a directory as long as there is more than one file to copy.
But to make it work even if copying just one file, you could simply create the directory first:
#echo off
mkdir c:\backupfolder 2>NULL
xcopy /me c:\test c:\backupfolder
The 2>NULL (redirecting standard error to NULL) suppresses the error message that occurs if the directory already exists.

Command line to change file extension of all files under a folders and its subfolders

I am looking for a command line which can change the file extension of all the files that are under a folder and its subfolders. Is there any way to do this?
I tried with ren *.js *.txt but this only changes the file extension of the files under one folder.
I assume by ms-dos you really mean the command prompt on Windows. Not many people still use ms-dos.
The following will run your REN command on each folder within the hierarchy that contains .js files. It is probably a bit more efficient then running REN for each file individually.
for /r %F in (.) do #if exist "%F\*.js" ren "%F\*.js" "*.txt"
Double up the percents (%F becomes %%F) if run within a batch script.
You could try this:
For /R %G in (*.js) do REN "%G" "%~nG.txt"
Note that you'll need to use %% instead of % if running from a batch file.

Copy a file to all folders batch file?

I need copy credits.jpg from C:\Users\meotimdihia\Desktop\credits.jpg to D:\Software\destinationfolder and all subfolders
I read many and i write
/R "D:\Software\destinationfolder" %%I IN (.) DO COPY "C:\Users\meotimdihia\Desktop\credits.jpg" "%%I\credits.jpg"
then i save file saveall.bat but i run it , it dont work at all.
help me write 1 bat
Give this a try:
for /r "D:\Software\destinationfolder" %i in (.) do #copy "C:\Users\meotimdihia\Desktop\credits.jpg" "%i"
Of course, if it's to go into a batch file, double the '%'.
This was the first search I found on google for batch file copy file to all subfolders.
Here's a way with xcopy.
There's also robocopy but that would be too powerful for a simple task like this. (I've used that for entire drive backups because it can use multi-threading)
But, let us focus on xcopy.
This example is for saving to a file with the extension .bat. Just drop the additional % where there is two if running directly on the command line.
cd "D:\Software\destinationfolder"
for /r /d %%I in (*) do xcopy "C:\temp\file.ext" "%%~fsI" /H /K
cd "D:\Software\destinationfolder" change directory to the folder you want to copy the file to. Wrap this in quotes if the path has whitespaces.
the for loop - See help here. Or type for /? in a command prompt.
/r - Loop through files (recurse subfolders)
/d - Loop through several folders
%%I - %%parameter: A replaceable parameter
xcopy - Type xcopy /? in the command line for lots of help. You may need to press Enter to read the entire help on this.
C:\temp\file.ext - The file you want to copy
"%%~fsI" - Expands %%I to a full pathname with short names only
/H - Copies files with hidden and system file attributes. By default, xcopy does not copy hidden or system files
/K - Copies files and retains the read-only attribute on Destination files if present on the Source files. By default, xcopy removes the read-only attribute.
The last two parameters are just examples if you're having trouble with any read-only files and will retain the most important file properties.
Lots more xcopy parameters here
xcopy examples here
Just for completeness. This example below will copy the same file in each folder of the current directory and not any sub-folders. Just the /r option is removed for it to behave like this.
for /d %%I in (*) do xcopy "C:\temp\file.ext" "%%~fsI" /H /K
If you can use it: Here is a PowerShell solution (PowerShell is integrated in Windows 7 and available from XP and up):
$file = "C:\...\yourfile.txt"
$dir = "C:\...\YourFolder"
#Store in sub directories
dir $dir -recurse | % {copy $file -destination $_.FullName}
#Store in the directory
copy $file -destination $dir
I'm pretty sure that the last line can be integrated in dir ... but I'm not sure how (I do not use PowerShell very often).

Resources