I am trying to copy some pictures from one folder and move it to another folder using batch. I am having trouble in setting the path since the path contains spaces in it.If I remove the spaces from folder it works fine but with spaces it gives error that cannot find the path.. here is the code.
#echo off
SET odrive=%odrive:~0,2%
setlocal enabledelayedexpansion
set backupcmd=echo
set backupcmd=xcopy /s /c /d /e /h /i /r /y
set "filesw=C:\Users\%USERNAME%\Numerical Analysis\*.png"
for /f "delims=" %%i in ('dir /s /b %filesw%') do (
if "%%~xi"==".pdf" set "dest=D"
if "%%~xi"==".docx" set "dest=P"
if "%%~xi"==".zip" set "dest=Z"
if "%%~xi"==".rar" set "dest=Z"
if "%%~di"=="C:" if "!dest!"=="Z" set "dest=!dest!3"
%backupcmd% "%%i" "%drive%\Personal\PICS\Wedding\Barat\MOVIE!dest!\"
)
#echo off
cls
It would be really helpful if you guys help me fix this path problem.
The solution to your problem, specifically "setting the path since the path contains spaces in it" leading to "cannot find the path" is to "quote the filespec" Thus:
for /f "delims=" %%i in ('dir /s /b "%filesw%"') do (
In this way, the string between the double-quotes is used literally (although certain special characters with a meaning to batch like &^)! need to be escaped; that is, preceded by a caret ^). As you have it, the dir command will be executed with multiple arguments since spaces are separators and the variable filesw will be substituted literally into the dir command before execution - and like most commands, dir uses spaces (commas, tabs, semicolons) as separators.
This is how I would do it. Use the FOR /R command to walk the directory tree looking for the file type you want. But I am just guessing at what you are trying to do.
#echo off
setlocal enabledelayedexpansion
set backupcmd=xcopy /s /c /d /e /h /i /r /y
set "filep=C:\Users\%USERNAME%\Numerical Analysis"
for /R "%filep%" %%i in (.) do (
if "%%~xi"==".pdf" set "dest=D"
if "%%~xi"==".docx" set "dest=P"
if "%%~xi"==".zip" set "dest=Z"
if "%%~xi"==".rar" set "dest=Z"
if "%%~di"=="C:" if "!dest!"=="Z" set "dest=!dest!3"
%backupcmd% "%%i" "%drive%\Personal\PICS\Wedding\Barat\MOVIE!dest!\"
)
Related
I'm trying to figure out how to match folders with a dot in the file name (e.g., ".svn") in a Windows batch script.
Here's the basic script:
setlocal
pushd c:\myDir
#echo off
FOR /D /r %%G in ("*\.whatever") DO (
echo %%G
REM do stuff
)
#echo on
popd
endlocal
This works just fine for most folder names (e.g., "*bin"), but I can't figure out the method to specify a folder with the dot. "*.whatever" and "*\.whatever" return no results. I'm guessing I'm missing some escape character or something equally simple, but I haven't been able to find any documentation on it.
(Before anyone asks, no I'm not trying to recursively delete subversion folders; "*.svn" is just an example.)
Maybe I am missing something, but as you say it seems simple
for /r /d %%a in (.*) do echo %%~fa
But if the folders are hidden, the normal for will not be able to see them, so we need to execute a dir command an process its output with a for /f
for /f "delims=" %%a in ('dir /ad /s /b .*') do echo %%~fa
On a PC with Windows 7, I'm using a simple batch script to rename some Excel files, pre-pending their parent folder name:
for /f "delims=" %%i in ('dir /b /AD') do (
cd "%%i"
for /f "delims=" %%j in ('dir /b *.xl*') do ren "%%~j" "%%i_%%~j"
cd..
)
I noticed that some files generated an error, "System cannot find the specified file." These files all had an em dash (—) in the filename. After I changed the em dash to a regular hyphen (-), using Windows Explorer, the script worked fine on these files.
How can I help script the rename of these files?
Your problem is not with the batch variables: from the command line this works fine:
for %i in (*) do ren "%~i" "test_%~i"
but, as can be seen from:
for /f "delims=" %i in ('dir /b /a-d') do #echo ren "%~i" "test2_%~i"
dir /b is changing the em dash to a hyphen, and so the ren command clearly won't find the file to change.
For your examples you should find:
for /d %%i in (*) do (
and
for %%j in (*.xl*) do ...
should work fine.
If you need the dir /b for other reasons, I don't see a solution right now.
(I had a convoluted attempt exchanging all hyphens for question marks, using the "Environment variable substitution" and "delayed environment variable expansion" as described in SET /? and CMD /?, allowing any character to match, and then again use ren pattern matching to ignore the problem.
I.e.
SETLOCAL ENABLEDELAYEDEXPANSION
for /f "delims=" %%I in ('dir /b /a-d') do (
Set K=%%I
ren "!K:-=?!" "test2_!K:-=?!"
)
but ren * x* replaces the start of the files with x, so the above replaces the hyphens with the content at that location before test_ was inserted.
So the best this approach can do is convert the em dashes to hyphens with:
SETLOCAL ENABLEDELAYEDEXPANSION
for /f "delims=" %%I in ('dir /b /a-d') do (
Set K=%%I
ren "!K:-=?!" "test2_!K!"
)
)
And confirming it is the output of dir /b that is the problem: on the command line:
dir /b *—* > test.txt
where that — is an em dash, will only list the files with em dashes, but, in the output file, e.g. Notepad test.txt, you'll only find hyphens, and no em dashes.
BTW I've done all this testing on Windows 8.1 which VER displays as Microsoft Windows [Version 6.3.9600].
(As I mention above I did have ren * x* in this answer, but that replaces the first character with x rather than insert it, which I always thought ren did!)
OK, I apologize ahead of time for a) using an old, crappy technology (BAT files) and b) asking what seems to be a redundant question. I'm limited in the technology I'm allowed to use in this particular case and after looking at dozens of posts on the subject I can't find anything I can adapt to what I need.
I have a directory structure that looks like this:
A
B
C
D
etc...
XYZ
more folders
My BAT file is located outside this files system. I need to inspect it starting at level "C" and need to find the "XYZ" directory. The folders between C and XYZ can have variable names depending on the environment in which the files were created. I need to end up with a string that consists of the directory names from C through XYZ (i.e. "C\D\E\F....\XYZ") that I can put into a variable so when my BAT file is completed I can reference the variable and run another command.
I've looked at posts using FIND and FOR but I can't seem to figure out how to a) limit the string to the starting directory (for example when I combine FOR with DIR I get "A\B\C...") and how to stop when I get to "XYZ"...
Any help is greatly appreciated.
This should work in most situations:
#echo off
setlocal enableDelayedExpansion
set "root=c:\a\b\c"
set "target=xyz"
for %%R in ("%root%") do for /f "delims=" %%F in (
'dir /b /s /ad "%root%\%target%"'
) do (
set "fullPath=%%F"
set "relpath=!fullPath:%%~dpR=!"
)
echo !relpath!
It can fail if any of your paths contain ! or =. There are solutions for this, but the code is significantly more complicated.
EDIT
Actually, there is a relatively simple solution using FORFILES that should work in all situations. (Assuming your version of Windows has FORFILES)
#echo off
setlocal disableDelayedExpansion
set "root=c:\a\b\c"
set "target=xyz"
for /f "delims=" %%F in (
'forfiles /p "%root%" /m "%target%" /s /c "cmd /c if #isdir==TRUE echo #relpath"'
) do set "relpath=%%~F"
for %%R in ("%root%") do set "relpath=%%~nxR%relpath:~1%"
echo %relpath%
The only restriction is the code has to change slightly if your result contains poison characters like &. In that case you need to add quotes to the final ECHO statement, or else enable delayed expansion at the end and use echo !relpath!
For a) question:
FOR /F "TOKENS=*" %%d IN ('DIR A\B\C\XYZ /S /AD /B') DO SET variable=%%d
For a) and b) question:
FOR /D /R "A\B\C" %%d IN (*.*) DO IF /I "%%~nxd"=="XYZ" (SET variable=%%d& GOTO :EOF)
but this will exit batch script, so you need:
... your batch code
CALL :GET_XYZ
... your batch code
GOTO :EOF
:GET_XYZ
FOR /D /R "A\B\C" %%d IN (*.*) DO IF /I "%%~nxd"=="XYZ" (SET variable=%%d& GOTO :EOF)
ECHO XYZ not found!
GOTO :EOF
I have almost no experience in batch, but now I need a script to delete all files that contain certain characters in their names from a folder and its subfolders on Windows 64. I only did simple things in batch like
del "C:\TEST\TEST2\*.TXT"
But I have no idea how to perform the filtering I need.
Could someone help me to achieve this using batch?
EDIT more exactly, the question is "how to tell batch to include subfolders?"
The /s switch exists on quite a few commands (including del)
del /s "C:\TEST\TEST2\*.TXT"
The help for del /? says:
/S Delete specified files from all subdirectories.
Try this:
#echo off & setlocal
set "MySearchString=X"
for /f "delims=" %%i in ('dir /b /s /a-d ^| findstr /i "%MySearchString%"') do echo del "%%~i"
Set the variable MySearchString to the character or string to search and remove the echo command, if the output is OK.
You can also specify the MySearchString for the file name only:
#echo off & setlocal
set "MySearchString=T"
for /r %%a in (*) do for /f "delims=" %%i in ('echo("%%~na" ^| findstr /i "%MySearchString%"') do echo del "%%~fa"
i need to copy test.swf to all subfolders of c:/test folder's that doesn't contain "git" word
just tried something like that but not worked
#setlocal enableextensions enabledelayedexpansion
#echo off
for /r %%a in (.) do
(
if not x%a:git=%==x%a% do
(
#copy "C:\test.swf" %a > nul
)
)
endlocal
There is certainly nothing wrong with using vbscript :-) (see OP's answer)
But I thought I would point out where your batch code went wrong.
The left paren must go on the same line as the IF or DO, and there must be a preceding space.
DO is not used with IF
You also need the FOR /D option to go along with /R. Without it you will get files instead of directories.
Your . will include the parent directory. You want * instead to get just the children.
I'm not sure about your requirement, but I assume a path like C:\test\_git_\test should get the file because the folder name does not contain "git" (though the parent does). Your code would look for git anywhere in the path.
All FOR variables must be referenced with double percents as in %%a when in a batch file. You were not consistent.
You cannot perform search and replace on a FOR variable, only on environment variables.
Additional points for improvement, though not errors:
It is extremely rare that enableExtensions is needed. It is enabled by default.
Better to put #echo off at the top then prefix each command with #.
You enabled delayed expansion, but did not use it. Although a correct solution using your algorithm would require it.
Here is the correct code for your algorithm. (actually none of the code solutions below have been tested, but I think I got them correct)
#echo off
setlocal enableDelayedExpansion
for /d /r "c:\test" %%F in (*) do (
set "name=%%~nxF"
if "!name:git=!" neq "!name!" copy "c:\test.swf" "%%F" >nul
)
The above usually works. But it fails if a folder name contain ! because delayed expansion would corrupt the expansion of %%F. The solution is to toggle delayed expansion on and off within the loop.
#echo off
setlocal disableDelayedExpansion
for /d /r "c:\test" %%F in (*) do (
set "name=%%~nxF"
setlocal enableDelayedExpansion
if "!name:git=!" neq "!name!" (
endlocal
copy "c:\test.swf" "%%F" >nul
) else endlocal
)
But there is a much simpler method. You can pipe the results of DIR to FINDSTR with a regex that will filter out folders with "git" in the name. Then use FOR /F to process the results.Edit - I simplified the regex.
#echo off
for /f "delims=" %%F in (
'dir /ad /s /b "c:\test\*" ^| findstr /virc:"git[^\\]*$"'
) do copy "c:\test.swf" "%%F"
The entire process can be done on one line from the command line
for /f "delims=" %F in ('dir /ad /s /b "c:\test\*" ^| findstr /virc:"git[^\\]*$"') do #copy "c:\test.swf" "%F"
just did it by vbscript
Const SourceDir = "C:\source"
Const TargetDir = "C:\target\"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(TargetDir)
Set colSubfolders = objFolder.Subfolders
Set dosyalarklasor = objFSO.GetFolder(SourceDir)
Set dosyalar = dosyalarklasor.Files
For Each objSubfolder in colSubfolders
if not instr(objSubfolder.Name,".git") > 0 then
For Each dosya in dosyalar
objFSO.CopyFile dosya, TargetDir & objSubfolder.Name & "\"
Next
end if
Next