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
Related
I am trying to replace URL encodings (e.g. %20 as a placeholder for a space) with their corresponding ASCII values in all filenames in a Windows folder and its subfolders.
If have a simple .bat file that can accomplish this, but it has limitations:
#echo off
Setlocal enabledelayedexpansion
Set "Pattern0=%%20"
Set "Replace0= "
Set "Pattern1=%%27"
Set "Replace1='"
Set "Pattern2=%%28"
Set "Replace2=("
Set "Pattern3=%%29"
Set "Replace3=)"
Set "Pattern4=%%5B"
Set "Replace4={"
Set "Pattern5=%%5D"
Set "Replace5=}"
For %%# in ("D:\Dropbox\Music\*.mp3") Do (
Set "File=%%~nx#"
Ren "%%#" "!File:%Pattern0%=%Replace0%!"
Ren "%%#" "!File:%Pattern1%=%Replace1%!"
Ren "%%#" "!File:%Pattern2%=%Replace2%!"
Ren "%%#" "!File:%Pattern3%=%Replace3%!"
Ren "%%#" "!File:%Pattern4%=%Replace4%!"
Ren "%%#" "!File:%Pattern5%=%Replace5%!"
)
Pause&Exit
There are two major limitations I'd like to fix:
It only checks the ..\Music\ folder root. I'd like it to look at
files in subdirectories, too.
It exits the For loop as soon as one of the renames are executed
(all %20's replaced first pass, for example, but nothing else).
And surely there is a better way to specify the encodings and their replacements (rather than variable pairs for each), but that's a nice-to-have feature.
These encodings always take the form %XX, where X are hexadecimal values.
To solve directory recursive search, you could use dir /s /b instead
FOR /F "delims=" %%# in ('dir /s /b "D:\Dropbox\Music\*.mp3"') Do (
...
)
Do not rename the file any time, only after all replacements
FOR /F "delims=" %%# in ('dir /s /b "D:\Dropbox\Music\*.mp3"') Do (
set "file=%%~#"
set "file="!File:%%20= !"
set "file="!File:%%28=(!"
...
move "%%~#" "!file!"
)
I'm using move here, because it works with full pathnames, too
To solve problems with exclamation marks in filenames/paths, you need to toggle delayed expansion
setlocal DisableDelayedExpansion
FOR /F "delims=" %%# in ('dir /s /b "D:\Dropbox\Music\*.mp3"') Do (
set "file=%%~#"
setlocal EnableDelayedExpansion
set "file="!File:%%20= !"
set "file="!File:%%28=(!"
...
move "%%~#" "!file!"
endlocal
)
i have loads of files which i want to organize differently. The batch script should create folders with the substring on the left side of the date in the filename.
Files are now named like this:
This_is_my_file_21.01.29_22-00_abc_115.avi
This_is_my_file_20.09.29_21-10_abc_15.avi
This_is_another_file_21.01.29_22-00_abc_55.avi
Pattern:
<Name with unknown number of underscores>_<YY.MM.DD>_<hh-mm>_<string with unknown length>_<number n from 1-999>.avi
Folders should be named like this:
This_is_my_file <- two files will go into this directory
This_is_another_file <- only one file.
The Problem is, how do I get the correct substring for my folder name?
This is what I have so far:
#echo off
setlocal
set "basename=."
for /F "tokens=1* delims=." %%a in ('dir *.avi /B /A-D ^| sort /R') do (
set "filename=%%a"
setlocal EnableDelayedExpansion
for /F "delims=" %%c in ("!basename!") do if "!filename:%%c=!" equ "!filename!" (
set "basename=!filename!"
md "!basename:~0,-23!"
)
move "!filename!.%%b" "!basename:~0,-23!"
for /F "delims=" %%c in ("!basename!") do (
endlocal
set "basename=%%c
)
)
#ECHO OFF
SETLOCAL
rem The following settings for the source directory, destination directory, target directory,
rem batch directory, filenames, output filename and temporary filename [if shown] are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files\t w o"
FOR /f "delims=" %%b IN ('dir /b /a-d "%sourcedir%\*.avi" ' ) DO (
SETLOCAL ENABLEDELAYEDEXPANSION
CALL :countus "%%b"
IF DEFINED subdir (
MD "!subdir!" 2>NUL
ECHO MOVE "%sourcedir%\%%b" "%sourcedir%\!subdir!\"
) ELSE (
ECHO Failed pattern check %%b
)
ENDLOCAL
)
GOTO :EOF
:: count number of underscores before pattern YY.MM.DD_hh-mm
:countus
SET /a ucount=0
:countusloop
SET /a ucount+=1
SET /a scount=ucount+1
FOR /f "tokens=%ucount%,%scount%delims=_" %%q IN ("%~1") DO SET "str1=%%q"&SET "str2=%%r"
IF NOT DEFINED str2 SET "subdir="&GOTO :EOF
:: is %str1%.%str2:-=.%. of form np.np.np.np.np where np is a number-pair?
SET "candidate=%str1%.%str2:-=.%."
FOR /L %%c IN (10,1,99) DO IF DEFINED candidate SET "candidate=!candidate:%%c.=!"&IF NOT DEFINED candidate GOTO success
FOR /L %%c IN (0,1,9) DO IF DEFINED candidate SET "candidate=!candidate:0%%c.=!"&IF NOT DEFINED candidate GOTO success
GOTO countusloop
:success
SET "subdir=%~1"
FOR /f "delims=:" %%e IN ("!subdir:_%str1%_%str2%=:!") DO SET "subdir=%%e"
GOTO :eof
The "move" command is merely echoed for verification. Remove the echo from echo move to actually move the files.
This possible solution uses the fact that your filenames have a known number of underscores if you work backwards. All I do is replace those underscores with backslashes, which obviously cannot already be contained in the filename. I can then use the relative paths to step up the filename, as if it were a directory tree, until all I have left is the part ahead of the date sequence, which I then replace the backslashes with underscores again. I use the result of that with robocopy, which has a move option, and will create the destination directory automatically, if it does not already exist. At the outset, I perform the directory search, in the same directory as the batch-file, using where.exe, (you can change that, on line three, from "%~dp0." to ".", if you want to use the current directory instead, or "any other path" as necessary). where.exe not only treats the ? wildcard as exactly one character, (unlike the dir command which is zero or one), but also ignores 8.3 naming. It therefore treats the .avi extension exactly as written, (and not 'beginning with' .avi, which dir, or a standard for loop, would).
Anyhow, feel free to give it a try:
#Echo Off & SetLocal EnableExtensions DisableDelayedExpansion
Set "}=" & For /F Delims^= %%G In ('(Set PATHEXT^=^) ^& %__AppDir__%where.exe
"%~dp0.":"?*_??.??.??_??-??_?*.avi" 2^> NUL') Do (Set "}=%%~nG"
SetLocal EnableDelayedExpansion & For %%H In ("\!}:_=\!") Do (
EndLocal & For %%I In ("%%~pH..\..") Do (Set "}=%%~pI"
SetLocal EnableDelayedExpansion & Set "}=!}:~1,-1!"
For %%J In ("!}:\=_!") Do (EndLocal & %__AppDir__%robocopy.exe ^
"%%~dpG." "%%~dpG%%~J" "%%~nxG" /Mov 1> NUL))))
If you want even further robustness, and do not wish to use a more suitable scripting technology, the following, extremely complex looking, version, is the same code, except that it uses findstr to validate the date and time sequence. It filters those avi files containing the following pattern, _yy.MM.dd_hh-mm_ in the avi filenames, using all dates from the beginning of 1970 up until the end of 2021:
#Echo Off & SetLocal EnableExtensions DisableDelayedExpansion
Set "}=" & For /F Delims^= %%G In ('(Set PATHEXT^=^) ^& %__AppDir__%where.exe
"%~dp0.":"?*_??.??.??_??-??_?*.avi" 2^> NUL ^| %__AppDir__%findstr.exe
/RC:"_[789][0123456789].0[123456789].0[123456789]_[01][0123456789]-[012345][0123456789]_"
/C:"_[789][0123456789].0[123456789].0[123456789]_2[0123]-[012345][0123456789]_"
/C:"_[789][0123456789].0[123456789].[12][0123456789]_[01][0123456789]-[012345][0123456789]_"
/C:"_[789][0123456789].0[123456789].[12][0123456789]_2[0123]-[012345][0123456789]_"
/C:"_[789][0123456789].0[123456789].3[01]_[01][0123456789]-[012345][0123456789]_"
/C:"_[789][0123456789].0[123456789].3[01]_2[0123]-[012345][0123456789]_"
/C:"_[789][0123456789].1[012].0[123456789]_[01][0123456789]-[012345][0123456789]_"
/C:"_[789][0123456789].1[012].0[123456789]_2[0123]-[012345][0123456789]_"
/C:"_[789][0123456789].1[012].[12][0123456789]_[01][0123456789]-[012345][0123456789]_"
/C:"_[789][0123456789].1[012].[12][0123456789]_2[0123]-[012345][0123456789]_"
/C:"_[789][0123456789].1[012].3[01]_[01][0123456789]-[012345][0123456789]_"
/C:"_[789][0123456789].1[012].3[01]_2[0123]-[012345][0123456789]_"
/C:"_[01][0123456789].0[123456789].0[123456789]_[01][0123456789]-[012345][0123456789]_"
/C:"_[01][0123456789].0[123456789].0[123456789]_2[0123]-[012345][0123456789]_"
/C:"_[01][0123456789].0[123456789].[12][0123456789]_[01][0123456789]-[012345][0123456789]_"
/C:"_[01][0123456789].0[123456789].[12][0123456789]_2[0123]-[012345][0123456789]_"
/C:"_[01][0123456789].0[123456789].3[01]_[01][0123456789]-[012345][0123456789]_"
/C:"_[01][0123456789].0[123456789].3[01]_2[0123]-[012345][0123456789]_"
/C:"_[01][0123456789].1[012].0[123456789]_[01][0123456789]-[012345][0123456789]_"
/C:"_[01][0123456789].1[012].0[123456789]_2[0123]-[012345][0123456789]_"
/C:"_[01][0123456789].1[012].[12][0123456789]_[01][0123456789]-[012345][0123456789]_"
/C:"_[01][0123456789].1[012].[12][0123456789]_2[0123]-[012345][0123456789]_"
/C:"_[01][0123456789].1[012].3[01]_[01][0123456789]-[012345][0123456789]_"
/C:"_[01][0123456789].1[012].3[01]_2[0123]-[012345][0123456789]_"
/C:"_2[01].0[123456789].0[123456789]_[01][0123456789]-[012345][0123456789]_"
/C:"_2[01].0[123456789].0[123456789]_2[0123]-[012345][0123456789]_"
/C:"_2[01].0[123456789].[12][0123456789]_[01][0123456789]-[012345][0123456789]_"
/C:"_2[01].0[123456789].[12][0123456789]_2[0123]-[012345][0123456789]_"
/C:"_2[01].0[123456789].3[01]_[01][0123456789]-[012345][0123456789]_"
/C:"_2[01].0[123456789].3[01]_2[0123]-[012345][0123456789]_"
/C:"_2[01].1[012].0[123456789]_[01][0123456789]-[012345][0123456789]_"
/C:"_2[01].1[012].0[123456789]_2[0123]-[012345][0123456789]_"
/C:"_2[01].1[012].[12][0123456789]_[01][0123456789]-[012345][0123456789]_"
/C:"_2[01].1[012].[12][0123456789]_2[0123]-[012345][0123456789]_"
/C:"_2[01].1[012].3[01]_[01][0123456789]-[012345][0123456789]_"
/C:"_2[01].1[012].3[01]_2[0123]-[012345][0123456789]_"') Do (Set "}=%%~nG"
SetLocal EnableDelayedExpansion & For %%H In ("\!}:_=\!") Do (
EndLocal & For %%I In ("%%~pH..\..") Do (Set "}=%%~pI"
SetLocal EnableDelayedExpansion & Set "}=!}:~1,-1!"
For %%J In ("!}:\=_!") Do (EndLocal & %__AppDir__%robocopy.exe ^
"%%~dpG." "%%~dpG%%~J" "%%~nxG" /Mov 1> NUL))))
As can be seen in the image I have folders with "." in them I would like to replace these with a "_" using CMD is there a method to do this.
cmd.exe shell scripting is the worst approach for anything more than #echo off :-)
But ok.
You can use the enhanced shell command set to replace characters in a variable:
set DUH=FBB
echo %DUH:B=O% -> FOO
So, for your problem, you need to read all folders and get them in a variable, so you can replace .=_ and then rename.
First batch: rena.cmd iterates over your folders
#echo off
for /D %%i in ( *.* ) do call rena2.cmd %%i
Second batch: rena2.cmd handles the rename
#echo off
setlocal enableextensions
setlocal enabledelayedexpansion
set TONAME=%~1
move %1 "%TONAME:.=_%"
exit /B
This can be done in one script, feel free to fiddle it together, I won't :-)
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir\t w o"
FOR /f "delims=" %%a IN (
'dir /b /ad "%sourcedir%\*.*" '
) DO (
SET "dirname=%%a"
SET "dirname=!dirname:.=_!"
IF "!dirname!" neq "%%a" ECHO(REN "%sourcedir%\%%a" "!dirname!"
)
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
The required REN commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(REN to REN to actually rename the files.
Dimply perform a directory-list, change the . to _ and if a change was made, perform the rename.
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!\"
)
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