Using cmd to move files with a specific filename mask - batch-file

I am trying to use a simple windows cmd command to move a set of .csv files from one directory to another.
I know that this can be easily achieved through:
MOVE "*.csv" "D:/lorik_home"
The issue comes when I want to only move some specific files with a filename mask, for instance, I want to move all files which are in the .csv extension and start with the name: lorik_files_. I already tried using:
MOVE "lorik_files_*.csv" "D:/lorik_home"
This works when there is only one file of the format: lorik_files_20112233_09_33_33.csv, but when there are two files with the masks such as:
lorik_files_20112233_09_33_33.csv
lorik_files_20112233_10_23_42.csv
I get an error such as:
A duplicate file name exists, or the file cannot be found.
I need a hand for capturing those filename prefix masks, conditioned by .csv extension at the end.

Related

Copy a file with a name criteria using WinSCP scripting

I have a Test folder that contains multiple file with two types of filenames as below:
Cycle2605.zip
Cycle2605_P.zip
Cycle2705.zip
Cycle2705_P.zip
What I manage to do is, move all the file from the folder to other server via WinSCP SFTP as shown in the code below.
open sftp://user:password#hostname/ -hostkey="ecdsa-sha2*******"
put D:\Users\AALADELA\Desktop\Test /cygdrive/d/VB_SHARE/astroQA/AFP/in
exit
But how do I move file that in the filename not contain _P to the destination instead of move all file?
open sftp://user:password#hostname/ -hostkey="ecdsa-sha2*******"
if <filename not contain _p> echo put D:\Users\AALADELA\Desktop\Test /cygdrive/d/VB_SHARE/astroQA/AFP/in
exit
You can exclude files matching certain pattern with -filemask switch:
put -filemask=|*_P.zip D:\Users\AALADELA\Desktop\Test /cygdrive/d/VB_SHARE/astroQA/AFP/in
Or you can pick only the files you want, if your file name convention allows, e.g.:
put -filemask=Cycle????.zip D:\Users\AALADELA\Desktop\Test /cygdrive/d/VB_SHARE/astroQA/AFP/in
In this case, it's easier to use a Windows wildcard directly in the source path:
put D:\Users\AALADELA\Desktop\Test\Cycle????.zip /cygdrive/d/VB_SHARE/astroQA/AFP/in/

Running SAVE AS using a batch file

I have a batch file that creates text files in multiple folders.
What I need it to do as well, is after creating that text file, save a copy of it as a .scr file
If I were to do with this without a batch file, I would open the text file, click SAVE AS and save the file with a .scr extension. I cannot figure out how to add this feature to my batch file however.
The original text file cannot be erased, so I can't just change the extension. I would have to copy it, then change the extension, or imitate the SAVE AS feature.
Help?
I just used the ren *<> *<> command. It is extremely redundant because I end up making two text files that are identical and just changing one, but it gets the job done

How do I copy existing files and create a duplicate files using xcopy?

I can't seem to find a way to copy an existing file but I do not want to let the previous file with the same name get overwritten. Let say I want to copy data.txt from various pc's by using batch file but I do not want "data.txt" gets overwritten, instead is there a way for the command to generate automated filename to avoid it from overwritten?
You can use the "/-y" switch to confirm before copying a duplicate file.
/-y : Prompts to confirm that you want to overwrite an existing
destination file.
The entire list of switches can be found here

List a directory folder to a variable

I felt it was better to ask this separately rather than expect an answer from my comment on my previous post.
I already have variables set for the directory number %jobn% which is unique is there a way I can search for the unknown element to add to another variable, I know via the command line I can run Dir D09854* and I will get a single report with the full name, can this be collected somehow and add to a named variable?
S:\SWDA\HBOS>dir d09854*
Volume in drive S is Images
Volume Serial Number is FE8F-38FE
Directory of S:\SWDA\HBOS
18/02/2013 10:29 <DIR> D09854_Parent Test
I want to add the elements after "_" to a variable %DirDesc% so I can create the full path by combining %jobn%%DirDesc% to get "D09854_Parent Test"
dir d09854* /b will recover the full folder name in one line, without the extra cruft, if that's any use? What are you writing this widget in?
Does it have to be Good Old Fashioned DOS, or can the newer Command extensions be used?
With limited old DOS, I can't think of a way to get that into a SET Variable without piping it to a temporary batch file, having first ECHO'd a set variable= into it, and using >> in the pipe to append to it... and then CALL the temporary batch file to execute the command!

How to exclude a specific file from being copied using xcopy

I'm new to Windows batch files, but I'm writing a .bat file that just copies a bunch of files from one place to another maintaining the file directory structure. Using xcopy this is simple but I need to exclude some files from being copied. You can use /exclude and create a text file full of strings that you want to be excluded, but this doesn't just exclude files with the exact names in the text file, it excludes all files whose filenames contain any of the strings in the text file.
What this means is, if I want to exclude any files named 123.txt and put this string in my exclusions text file, if there was a file called 1123.txt anywhere in the source folder or any of its subfolders that would also be excluded.
How can I exclude only files with a specific filename from being copied?
Evening Bill.
Can you add a slash before each file name? That should work
EG
instead of
123.txt
blah.txt
use
\123.txt
\blah.txt
Try creating a temporary folder, xcopying all of the files into that folder, deleting the ones you don't want, then xcopying those to the final destination. Finally, delete the temporary folder and its contents with rd xyzzy /q/s

Resources