shell - Windows Batch sript that moves files based on a (partial char string) looking it up in a CSV/txt file -
what i'm looking might variation of solution: windows batch file sort files separate directories based on types specified in csv
my situation: batch process in server creates files this: s0028513-010716-0932.txt. s stands summary, first 5 digits stand supplier, last 2 before hyphen stand distribution center. after hyphen, there date , after second hyphen timestamp.
what need is:
- set variable month/year (e.g. 0716) (this has been set "set /p c:please enter mmyy:"). this part done.
- create folder subfolders (e.g. 0716\pharma, 0716\medical, etc). i've done part.
- look supplier number in csv file (e.g. s00285 above) and
- move file corresponding folder based on mmyy\pharma, etc.
points 3 , 4 obvioulsy missing. practical example: there 3 folders files can moved: pharma, medical , consumer
the csv file looks this:
- s00285 consumer
- s00286 pharma
- s00287 medical
- ...
what want script month/year combination in variable c , take files correspond month/year , move them 3 folders according assignment in csv file.
can done standard windows scripting? sorry guys, i'm novice can tell. have basic knowledge of bash scripting.
thank lot advice.
br marcio
this can accomplished powershell
$folderroot = "e:\target\directory" set-location $folderroot # 1. have user input month/year string do{ $mmyy = $(read-host 'please enter mmyy').trim() } until ($mmyy -match '\d{4}') # 2. create directory mkdir $mmyy # ?. gather input files year $files = get-childitem -filter s*.txt |where-object {$_.basename -match "s\d{7}-\d{2}$mmyy-\d{4}"} # ?. load csv file hash table supplier numbers $supplierlookuptable = @{} # assuming csv has headers: supplier,industry import-csv -path e:\path\to\suppliers.csv |foreach-object { $supplierlookuptable[$_.supplier] = $_.industry } foreach($file in $files) { # grab s , first 5 digits file name $supplier = $file.basename.substring(0,6) # 3. industry $industry = $supplierlookuptable[$supplier] $destination = join-path $mmyy $industry # create folder if doesn't exist if(-not (test-path $destination)) { mkdir $destination } # 4. move file move-item $file.fullname -destination $destination }
Comments
Post a Comment