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