copy a set of template file and change the filename by command batch - wildcard

I have some template file in c:\Temp, such as abcMMDD.txt, xyzMMDD.txt, I already use command batch get the current date varible, by fail to copy the file by wildcard.
I just want copy those file to c:\report\
set MM=%date:~3,2%
set DD=%date:~0,2%
set MMDD=%MM%%DD%
xcopy c:\Temp\*MMDD.txt "c:\report\*%MMDD%.txt"
I hope the c:\result\abc0908.txt , xyz0908.txt

Related

How to use SQLite db file location in select?

I would like to use the main database file location in function (replace or other) as input data. How can i get it?
for example: replace('c:\temp\main.db','main.db','')
I'm creating a database script for command line use under win10.
.database command result seems to be fine, but how to use in replace input?
EDIT: I have to use the location as variable, because i run the script from command line, recursively in a lot of directories with a lot of small databases, so i can't use constant value in replace.
The cmd line command:
forfiles /p c:\temp /m files.db /s /c "cmd /c c:\sqlite3.exe #path < c:\BookDbDataCopy.txt"
I want to use the path in the select as variable.
I got it :)
create table path (a text);
.mode csv
.output 'c:\data.csv'
.databases
.import 'c:\data.csv' path
The solution:
- create a table for the path
- write the command output in file
- import the file in table
- select the table in replace variable :)
select replace((select a from path where a like 'main%'),'main: c:\',''), t.* from path;

Check if a file is open using Windows command line within R

I am using the shell() command to generate pdf documents from .tex files within a function. This function sometimes gets ran multiple times with adjusted data and so will overwrite the documents. Of course, if the pdf file is open when the .tex file is ran, it generates an error saying it can't run the .tex file. So I want to know whether there are any R or Windows cmd commands which will check whether a file is open or not?
I'm not claiming this as a great solution: it is hacky but maybe it will do. You can make a copy of the file and try to overwrite your original file with it. If it fails, no harm is made. If it succeeds, you'll have modified the file's info (not the contents) but since your end goal is to overwrite it anyway I doubt it will be a huge problem. In either case, you'll be fixed about whether or not the file can be rewritten.
is.writeable <- function(f) {
tmp <- tempfile()
file.copy(f, tmp)
success <- file.copy(tmp, f)
return(success)
}
openfiles /query /v|(findstr /i /c:"C:\Users\David Candy\Documents\Super.xls"&&echo File is open||echo File isn't opened)
Output
592 David Candy 1756 EXCEL.EXE C:\Users\David Candy\Documents\Super.xls
File is open
Findstr returns 0 if found and 1+ if not found or error.
& seperates commands on a line.
&& executes this command only if previous command's errorlevel is 0.
|| (not used above) executes this command only if previous command's errorlevel is NOT 0
> output to a file
>> append output to a file
< input from a file
| output of one command into the input of another command
^ escapes any of the above, including itself, if needed to be passed to a program
" parameters with spaces must be enclosed in quotes
+ used with copy to concatinate files. E.G. copy file1+file2 newfile
, used with copy to indicate missing parameters. This updates the files modified date. E.G. copy /b file1,,
%variablename% a inbuilt or user set environmental variable
!variablename! a user set environmental variable expanded at execution time, turned with SelLocal EnableDelayedExpansion command
%<number> (%1) the nth command line parameter passed to a batch file. %0 is the batchfile's name.
%* (%*) the entire command line.
%<a letter> or %%<a letter> (%A or %%A) the variable in a for loop. Single % sign at command prompt and double % sign in a batch file.
.
--

How do I copy files recursively through a directory tree while appending parent folder names to the new files? Batch or VBScript

What I'm trying to do is copy a couple hundred images scattered throughout a directory tree and copy them all to another, single folder. I've found out how to do that with 'for /r %i in (*.jpg) ', but my main issue is that the files are all named using the same conventions. Sample:
Parent_Folder
001
file1.jpg
file2.jpg
002
file1.jpg
file2.jpg
The list continues as such. What I'm aiming to do is create this:
Dest_Folder
001_file1.jpg
001_file2.jpg
002_file1.jpg
002_file2.jpg
where the file's parent directory's name is added to the front of the files (so as to keep them listed in the same order). Not even individually going through every folder and using the "copy & rename" option will work since that will list all files named "file1.jpg" one after the other, "file2.jpg", and so on.
If there's any way to do this in a batch file, that would be much preferred, but a VBScript wouldn't be a bad choice either.
Thanks for any help!
EDIT
Magoo has given the perfect answer for my issue, but it seems his script only works if the source & destination paths have no spaces in them, despite the double quotes. I tried using this same script on another image collection, but with spaces in the paths, and I'm given an error saying the '[word after the first space in the path] was not expected at this time'. It was simple enough to just rename the folders, perform the copy, the rename them back, but is there a workaround for this?
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=c:\sourcedir"
SET "destdir=c:\destdir"
FOR /r "%sourcedir%" %%a IN (*.jpg) DO (
SET "newname=%%a"
SET "newname=!newname:*%sourcedir%\=!"
ECHO COPY /b "%%a" "%destdir%\!newname:\=_!"
)
GOTO :EOF
Grab each filename, remove the source directory name and a \, then copy the file to that name, changing each \ to _.
The required commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO COPY to COPY to actually copy the files.
Edit : fixed to allow spaces in sourcedir (simply quote after the /r in the for)
Sub BuildFileName(ParentFolder)
Dim objFSO, objFolder, strFileName
Set objFSO = WScript.CreateObject("Scripting.Filesystemobject")
Set objFolder = objFSO.GetFolder(ParentFolder)
For Each folder In objFolder.SubFolders
For Each file In folder.Files
strFileName = ParentFolder & "_" & folder.Name & "_" & file.Name
WScript.Echo strFileName
Next
Next
End Sub
Call BuildFileName("c:\scripts")

tcsh creating backup files

I'm trying to write a script that backs up a file which is given as parameter, in a way that a running number should be added to each copy of the file. For example, if the name of the original file was aa.c, then the first backup copy will be called aa.1.c. In the next time backup is run, the copy should be called aa.2.c, then aa.3.c, and so on. In addition ,the script should automatically find the copy with the highest number, and use it to create the new number.
Anyone know how can I do that with foreach loop?
Anyone know how can I do that with foreach loop?
#!/usr/bin/env tcsh
foreach file ($*:q)
# numb=1
while (-e $file:r.$numb.$file:e)
# numb++
end
cp -p $file $file:r.$numb.$file:e
end

Which file contains asterisk in unix

All
Here i post my doubt about files with asterisk, First i create one file
touch test*
If i check ls -lrt it shows the test* file in the current.
Then i removed that file using rm *.
Then i create two files which are test1* test2* using same touch command.
If i check ls -lrt.
It displays the two files that are test1* and test2*.
Again i create test* using touch command , now i check ls -lrt.
This time it will not display the file test*.
Why the test* file is not listed ?
Thanks & regards
As * is used by your shell as universal character, when you write
touch test*
your shell will tranform it into
touch test1* test2*
If you want to create 'test*', use simple quote, which inhibit the globing function.
touch 'test*'
Normally touch command is used to create a empty file.
if file is already there it will change only the access time.
first time you are using the touch test*
that there is no test file so it will creates file names as test*
second time you are using touch test* that, time the current directory having test1* and test2* files,
so it will expand into test1* and test2* .
In that case it will change only the access time of the both file.
if you want to create one more time test* file means you can use double quotes.

Resources