[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 402
  • 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
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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