Reputation: 828
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
Upvotes: 0
Views: 3765
Reputation: 130919
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.
.
will include the parent directory. You want *
instead to get just the children.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.Additional points for improvement, though not errors:
@echo off
at the top then prefix each command with @
.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"
Upvotes: 5
Reputation: 828
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
Upvotes: 1