Windows Batch - separate directory and filename from full file path string - windows

I'd like to separate both a filename and a directory string from inside a full file path variable so i can refer to each separately later in a batch script.
Input Variable: SET "FULL=C:\test\file.txt"
Wanted Output:
FILE: file.txt
PATH: C:\test\
Currently the for loop & syntax is not making a whole lot of sense to me (in this batch scripting language) which is making it harder for me to find a working solution online...

set "FULL=C:\test\file.txt"
for %%a in ("%FULL%") do (
set "filePath=%%~dpa"
set "file=%%~nxa"
)
for loop will iterate over a set of files (only one file in set in this case), and for each of them the code after the do clause is executed.
For each iteration of the for loop and so for each execution of the do clause, the replaceable parameter (the %%a in the previous code) will hold a reference to the file being processed.
This replaceable parameter has some modifiers (that can be seen running for /?) to retrieve the required information from the file. The modifiers are in the form
%% ~ modifier replaceableParameter
In the previous sample code, d modifier is the drive where the file is stored, p is the path (folder hierarchy) where the file is stored, n is the file name without extension and x is the extension. So
%%~dpa = drive and path of the file being referenced by a
%%~nxa = name and extensions of the file being referenced by a

Related

Windows batch script: saving a list of selected file to new text file using ECHO

I have provided all the detail and code below, but, the problem is only in the last line.
Background
I am creating a script named, GetSelectedFiles.cmd, whose shortcut is added to Windows' "Send To" context menu. For example, by copying the shortcut of above file to location: c:\users\user\AppData\Roaming\Microsoft\Windows\SendTo.
Purpose:
The goal of that script file is to get the list of selected file names when the user goes to the context menu and selects Send To > GetSelectedFiles.cmd.
Requirement:
1) The the selected file names will be separated by new lines.
2) The list will only contain the bare file name and extension (no path).
3) The list will be a saved inside the same directory as selected files.
4) The list will will be saved in a file whose file name matches the first selected file and has extension of `.selection`.
Example:
Assume you are in directory c:\users\u\. Say this directory has files named: w.txt, x.txt, y.txt, z.txt, with some other files.
User selects those above named 4 files, right clicks and does Send To > GetSelectedFiles.cmd.
After completing the above steps, the directory should have a new file named w.selection and it should contain following lines
w.txt
x.txt
y.txt
z.txt
It is a basic task, but, where I am having problem is the last line, specifically %firstFile% just returns empty. What am I missing?
::::::::::::::::::::::::::::::::::::
:::: Here is the complete code :::::
setlocal EnableDelayedExpansion
:: For new line const
set LF=^
for %%A in (%*) do (
IF NOT DEFINED firstFile (SET firstFile=%%~nxA)
::: This last line is the problem!
echo %%~nxA ^%LF% >> %%~dpA%firstFile%.selection
)

Batch command XCOPY prompts me: Is the destination file or directory

When I run this file:
xcopy .\*.odt .\source.zip
I am prompted to specify what source.zip is:
xcopy .\*.odt .\source.zip
Does .\source.zip specify a file name
or directory name on the target
(F = file, D = directory)?
In my case when it find the .odt file to copy the file and place in the same directory but with new name source.zip. Is there approach to avoid the prompting since I always want destination to be a file not directory.
Any .odt file (being in .zip format in fact) is a binary file, see OpenDocument Document Representation
As a collection of several sub-documents within a package, each of which stores part of the complete document. This is the common
representation of OpenDocument documents. It uses filename extensions
such as .odt, .ott, .ods, .odp ... etc. The package is a
standard ZIP file with different filename extensions and with a
defined structure of sub-documents. Each sub-document within a package
has a different document root and stores a particular aspect of the
XML document. All types of documents (e.g. text and spreadsheet
documents) use the same set of document and sub-document definitions.
Therefore, you need to treat it as a binary file (read copy /?):
copy /B .\*.odt .\source.zip
Above command would work smoothly only if there will be only one file with extension .odt. Otherwise, it will prompt you for Overwrite .\source.zip? (Yes/No/All):. To stay on the safe side:
from command line for %G in (.\*.odt) do copy /B "%G" ".\source_%~nG.zip"
from a batch script for %%G in (.\*.odt) do copy /B "%%G" ".\source_%%~nG.zip"
%~nG (or in batch %%~nG) explanation: read Parameter Extensions.

CMD to pick up specific files using a wild card

I have a CMD script I have been working on that reads a folder that has 3 different files in it. They have names like the following:
File1_ddmmyyyy.txt
File2_ddmmyyyy.txt
file3_ddmmyyyy.txt
I can use * to identify the files but the next step that uses the file name (Launching an ETL program) requires that the full file path. It is unable to identify the file (is entered as: file3_*.txt Literally)
Does anyone have and ideas on how I can grab this file based on the Wildcard, then obtain the full name to use later?
Thanks
EDIT - Example-
There is one ETL File executable line that matches up with each of the files based on the file name. for example:
File1_ddmmyyyy.txt is matched with ETL file File1.tf.map (Based on the file name and ETL file name) it is a one to one match (3 files to load and 3 ETL files to load them)
DJENGINE -sc "Y:\FileLocation\%Year_Mo_Da%\File1*.txt"-tc Server="X";Database="Y";Table="dbo.File1" "Y:\MapLocation\File1.tf.xml" (FileName = File1_ddmmyyyy.txt)
DJENGINE -sc "Y:\FileLocation\%Year_Mo_Da%\File2*.txt"-tc Server="X";Database="Y";Table="dbo.File2" "Y:\MapLocation\File2.tf.xml" (FileName = File2_ddmmyyyy.txt)
DJENGINE -sc "Y:\FileLocation\%Year_Mo_Da%\File3*.txt"-tc Server="X";Database="Y";Table="dbo.File3" "Y:\MapLocation\File3.tf.xml" (FileName = File3_ddmmyyyy.txt)
In the executable line of code for the ETL program instead of referencing the actual file name (Needed to run) it is trying to run File1*.txt Not filling in the variable.
My only thought would be since I have only 3 files and three ETL files that match base on the root file names I can place each of the files full names in a variable prior to this then use each of them in the executable ETL lines.
Not sure if this would work or how to do it. Let me know if this helps.
Hmm - started off clear as mud.
Then Taz arrived with his mixmaster on Turbo...
What congeals from this appears to be:
There's a directory "Y:\FileLocation\yyyy_mm_dd which contains 3 text files FileNddmmyyyy.txt
where dd is day number, mm month number, yyyy 4-digit year number and N will be 1..3.
The requirement from there is to build an appropriate command using the template
DJENGINE -sc "Y:\FileLocation\yyyy_mm_dd\FileN_ddmmyyyy.txt" -tc Server="X";Database="Y";Table="dbo.FileN" "Y:\MapLocation\FileN.tf.xml"
where dd,mm,yyyy,N have the same meanings, for N=1..3
#ECHO OFF
SETLOCAL
SET "yyyymmdd=%1"
IF NOT DEFINED yyyymmdd for /f "skip=1 delims=" %%x in ('wmic os get localdatetime') do set yyyymmdd=%%x&GOTO gotdate
:gotdate
SET "yyyymmdd=%yyyymmdd:~0,8%"&SET "yyyy=%yyyymmdd:~0,4%"&SET "mm=%yyyymmdd:~4,2%"&SET "dd=%yyyymmdd:~6,2%"
FOR %%a IN (1,2,3) DO ECHO(DJENGINE -sc "Y:\FileLocation\%yyyy%_%mm%_%dd%\File%%a_%dd%%mm%%yyyy%.txt" -tc Server="X";Database="Y";Table="dbo.File%%a" "Y:\MapLocation\File%%a.tf.xml"
GOTO :EOF
In the absence of any information about where yyyymmdd come from, the above should fill in the values for today.
If the procedure is provided with a parameter in the format yyyymmdd like this:
thisbatch 20140726
then the procedure will generate lines for July 26th 2014 (note that the value provided here is not checked for validity)
The required DJENGINE commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(DJENGINE to DJENGINE to actually execute the program on the files. This presumes that DJENGINE is an executable, not a batch procedure.

Renaming files of variable length in DOS

I'm trying to rename files that have '#' to '_'.
I understand that there is a straigt forward way of replacing the nth character in a file.
How do we rename files , if # symbol is present in different places in different files
For example, assuming the below files are present in a directory
a#file.txt
asdf#kfi.png
uiuydfjfk#kdi.txt
I want the output to be like this one
a_file.txt
asdf_kfi.png
uiuydfjfk_kdi.txt
Is there anyway to accomplish this ?
This uses a helper batch file called repl.bat from - http://www.dostips.com/forum/viewtopic.php?f=3&t=3855
It creates renfile.bat for you to examine for errors, and then execute.
dir *#* /b |repl "(.*)(#)(.*)" "ren \x22$&\x22 \x22$1_$3\x22" x >renfile.bat

How to continuously run a exe within a batch file with different parameters

I have an exe application that takes in 2 parameters. One is a input file path with an specific extension (e.g. *.jpg) and Second is the output file path.
Now in a folder, I have let's say 100 jpeg images which I want to pass in continuously and saved the output with the same file name as the input (extension will be different, the exe does the conversion).
Any idea how do I write a batch file to achieve this?
Thanks and Regards,
Perumal
Try this:
#FOR %%1 IN (%1) DO convert %%1 %2\%%n1.png
To be used as:
bulkconvert c:\test\*.jpg c:\test
It'll call convert for each file that matches the search pattern c:\test*.jpg and a 2nd parameter will be provided with the path provided as batch's 2nd parameter (note: there is not the trailing backslash) with the same file name but with extension png.
As reference: How to get folder path from file path with CMD

Resources