I have a big folder of files and I would like to find and copy specific files. There are no subfolders - all files are in the same path.
The problem is - a part of the file is always different and it is impossible to guess, so using the wildcard is required.
Here's a script:
dir K:\ALL_FILES /s/b|for /f %%i in ('find "First File To Copy.*.jpg"') do copy "%%i" k:\COPIED_FILES
dir K:\ALL_FILES /s/b|for /f %%i in ('find "Second File to be copied.*.jpg"') do copy "%%i" k:\COPIED_FILES
The above script works if I replace * with the real part of the file name.
I would like to do it in a batch file, but may be it would be easier to do with Powershell?
CodePudding user response:
The fact that all your files are in a single folder makes this task easier, since most PowerShell cmdlets that take a -Path
parameter accept string arrays for the parameter. So if you execute the cmdlet from the directory containing your files, the -Path
array can contain your wildcard match patterns. This allows all the filtering to be done by the FileSystem Provider, which is faster than using -Include
or -Exclude
parameters or piping files to Where-Object
.
$SourceDir = 'k:\ALLFILES`
$DestDir = 'k:\COPIED_FILES'
$MatchPatterns = @('First File To Copy.*.jpg','Second File To Copy.*.jpg')
Set-Location $SourceDir
Copy-Item -Path $MatchPattterns -Destination $DestDir
If you're just working interactively at the console, you can reduce the typing burden using aliases, positional parameters, and literal parameter values:
$MatchPatterns = @('First File To Copy.*.jpg','Second File To Copy.*.jpg')
sl k:\ALLFILES
copy $MatchPattterns k:\COPIED_FILES
And if you have a larger set of match patterns, a multiline Here-String makes for easier creation/editing:
$MatchPatterns = @'
First File To Copy.*.jpg
Second File To Copy.*.jpg
...
Tenth File to Copy.*.jpg
' -split "`n"
Documentation: