Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 387
  • Last Modified:

batch command to move file based on criteria in file name

I have a need to move a file from one folder to next based on criteria in a file name. When a file is imported, a date and timestamp is appended to the file like this: 20150107-0853_daxwms. If the same file is imported again it will look like this
20150107-1204_20150107-0853_daxwms. So what i am trying to do is only move the file if the date and timestamp only exist once.

The file name will always have "daxwms" after the Date/Timestamp. So since an underscore separates the date/timestamp i was thinking the best way is "if 2 underscores exist before daxwms then don't import." keeping in mind that underscores may exist after the daxwms.  

Thanks for any help you can give.
0
jacobJL
Asked:
jacobJL
  • 3
  • 2
1 Solution
 
NVITCommented:
Do you have a .bat file doing this?
This would stamp it if the filename pattern DOES NOT exist:
if not exist ????????-????_daxwms (
  command 1
  command 2
  ...
)

Open in new window

0
 
jacobJLAuthor Commented:
Yes i want this in a bat file.
But i don't think this will work because ????????-????_daxwms will always exist. The problem is when the file is reimported the date is appended again.

What i don't want to do is import the file more than twice. So i am looking to only move the file if the date is appended only once. Does this make sense?
0
 
jacobJLAuthor Commented:
let me explain how our imports work.
The file goes into an input folder, then once processed goes either to a archive folder or dump folder. If it goes to the dump folder i want to reimport the file into the input folder. If it dumps again i no longer want to import it.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
oBdACommented:
How about this? It's in test mode and will only display the move command it would normally run; remove the uppercase ECHO in line 7 to run it for real:
@echo off
setlocal
set SourceFolder=C:\Dump
set TargetFolder=D:\Input
set FileMatch="^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]_daxwms.*$"
for /f "delims=" %%a in ('dir /a:-d /b "%SourceFolder%\*.*" ^| findstr.exe /i /r %FileMatch%') do (
	ECHO move "%SourceFolder%\%%a" "%TargetFolder%"
)

Open in new window

Edit: note that the ".*" at the end of the FileMatch variable is not an extension wildcard; the whole string is a regular expression, and the ".*" will "eat" the complete rest of the file name after the date and the daxwms.
0
 
jacobJLAuthor Commented:
Thank you this worked great! I had a feeling regex would be the best solution here. and thanks for the extra note, that's definitely something i will keep in mind for future use.

NewVillageIt, thank you as well. I just couldn't get yours to work right.
0
 
NVITCommented:
No worries, mate. Have a good one.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now