List a directory folder to a variable - batch-file

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!

Related

Batch File Calls VB Script & Passes Filename as String Only

I have a batch file that calls a VB Script file. This VB Script comes from MS Office Suite and is located in the Installed Folder. My file calls the VB Script and pipes it to a file using the > symbol. This works if your redirect is a string. I was wondering how to redirect to a file using a variable.
I'm only including a small fraction of the code showing how I want it to work.
This, as written, works. However I want my batch file to use a variable instead of the text in quotes. In fact, my batch file creates the path depending on where the batch file is located.
::Create file and display on screen
Echo Writing Information to File: "OfficeStatus.txt"
Call cscript ospp.vbs /dstatus > OfficeStatus.txt
Echo.
When written as shown below, it does not work:
::Create file and display on screen
Echo Writing Information to File: "OfficeStatus.txt"
Call cscript ospp.vbs /dstatus > %_sLogFile%
Echo.
I don't know if the issue is because of the redirect not able to handle a variable or not. An example of what might be in the variable:
_sLogFile=E:\UserName\Documents\Status\OfficeStatus.txt.
Any thoughts would be great.
The variable, by the way, correctly contains the path and filename... so that's not the issue.
it does not work means did not do what I expected and unfortunately does not tell us what it did do.
Did it crash the system? Create an error message (If so, what message)? Create an output file in an unexpected place??
Personally, I'd suspect what you have concealed as username. Without knowing precisely what that text is, we start guessing, which isn't a logical approach. It doesn't have to be the real username, substitute Fred Bloggs for the real name to mask it if necessary.
I believe that username may either be %username% to retrieve the name from the system, or a real literal. The problem with this is that such names often contain spaces, you'd need to "Enclose the full filename in quotes".
See - had you told us _sLogFile=E:\Fred Bloggs\Documents\Status\OfficeStatus.txt it would all have been a lot clearer - presuming that my guess as to the cause of the problem is correct.
Perhaps you should look for files named like E:\Fred which is where the expected output may have ended up.

Changing file name while copying files in batch script

I want to copy the files from current folder to another location like (CURRENT FOLDER) file name like this (F0#CGDBANG000947532#) to another location like (\10.10.10.1\BasketsIn) with user name in the file name like (F0#CGDBANG000947532#logesh) at the end F0#CGDBANG000947532# copy to F0#CGDBANG000947532#username
thanks
Easy:
copy "c:\A" "d:\%username%_A"
EDIT
finally, after some of your comments here and in your other question, I understood your request (I think).
#echo off
for %%i in (%*) do if /i "%%~xi"==".eps" copy "%%i" "\\10.10.14.13\adman\in\displ\%%~ni%username%.%%~xi"
pause
If your file names do not have an extension or if you want the user name to be appended after the very end of the name (i.e. even after the extension), you could just use the following simple command:
COPY * \10.10.10.1\BasketsIn\*%USERNAME%
where USERNAME is a system environment variable that resolves to the user name of the current user.
If, however, you have names with extensions and you want the user name to be appended after the file name but before the file extension, you could use the ? mask character like this:
COPY * \10.10.10.1\BasketsIn\???????????????????????????????%USERNAME%.*
Just make sure you have provided enough ?s to cover the longest possible name in your case. If you are interested, you can learn more about this method in this excellent, in-depth Super User answer by dbenham:
How does the Windows RENAME command interpret wildcards?
One more note: this method may not work as expected with file names that have "multiple" extensions, i.e. like some.txt.doc.

Could someone help to understand this batch script?

I'm reading a batch file, but I do not understand it, can someone help to explain?
As I understand %0 is is the name of batch file, can we iterate on it? or is it a convenient way to represent a folder?
I can not find the variable %BatchPath% in the file, where do you think it's defined?
And it seems APATH is defined in the two loops?
for %%x in (%0) do set APATH=%%~dpsx
for %%x in (%BatchPath%) do set APATH=%%~dpsx
pushd %APATH%
You can iterate over a single value. It just means the set statement is executed once. The ~dps then strips the file name, so that only the directory remains.
The first line performs this action on %0, indeed the path and name of the current script.
The second line performs the same action on a given variable, Now that is the fun part, because if %BatchPath% is empty, nothing gets iterated, so the set statement on that line is not executed at all.
So effectively, it stores a directory, which is the directory of the script by default, but can be overridden by explicitly assigning a path to %BatchPath% before calling this script.
pushd allows you to save a directory, so you can return to it later using popd. It allows the script to jump to another directory an be able to restore the shell to the original directory before it terminates.
%0 is the current batch file.
%%~dpsx gives the current batch file's
short path here its giving the Drive name for eg "D:\"
Pushd Stores the name of the current directory for use by the popd command before changing the current directory to the specified
directory.
APATH is some variable used to store the path.
so basically the script is fetching details about the script file name , its drive location and storing it to be used as location from which last batch file ran or something like that.

.bat file to rename and move files with prompt

I am completely new to this, but I am trying to create a .bat file that will allow me to rename a pair of files within a designated folder and move them into a subfolder. The part I am having trouble with is that I am wanting a prompt to come up to identify/select the files to be renamed and moved.
Example file names are:
A1234, A1235, A1236, B1234, B1235, B1236, etc.
Is there a way to bring up a prompt that allows the user to type the shared name (ex 1234)of the files and rename and move both files to the designated subfolder?
Any and all help would be appreciated!
Suggested approach
for part of problem
part I am having trouble with is that I am wanting a prompt to come
up to identify/select the files to be renamed and moved. Is there a
way to bring up a prompt that allows the user to type the shared name
(ex 1234)of the files and rename and move both files to the designated
subfolder?
Do a search operation using wildcard, like "?1234" for the case highlighted above ( should be made generalized for all acceptable and expected patterns "*1234*" is the generic most )
Now do a RENAME inside a For loop on the results obtained by search.
As you suggest you are a newbie with Batch, following tutorials will help you build your file. Look for elements like Variables, For Loop
Batch Tutorial
Here you go
#echo off
set /p file=Please type shared name:
for %%a in (C:\Folder\?%file%.*) do (
move "%%a" subdir
ren "subdir\%%a" newname.*
)

DOS command to move all files in subdirectories one level up without overwriting same file name, unique size

MY QUESTION:
I have the same situation as Rishi. I have a number of versions of the same song by the same artist that appear on different CD's. If I use the batch command as written, will DOS overwrite songs with the same name, even if the file size is different for each unique file?
PREVIOUS QUESTION: DOS command to move all files in subdirectories one level up
REFERENCE Rishi asked the question on Jan 15th:
"I have a folder with a lot of sub folders with one or more files in each. I am trying to write a batch file that moves all those files to C:\songs (for example).
C:>FOR /R C:\Test %i IN (*) DO MOVE %i C:\Songs
The folders Test and songs exist, but I get an error saying
%i was unexpected at this time.
What am I doing wrong?"
ANSWER WAS
"FOR /R %i IN (C:\Test*) DO MOVE "%i" C:\Songs
In a batch file, it has to be %%i. Weird quirk of batch."
Within a given folder there can only be one version of a file with a given name. When executed within a batch, the MOVE command will automatically overwrite any pre-existing file of the same name. So the answer to your question is - YES, a file with the same name will be over-written, even if it has a different file size. (Note - if you are using Windows XP then you are not using DOS)
You can prevent a batch move from overwriting an existing file by piping N to MOVE with the -y option:
echo n | move /-y "%%~i" "C:\songs\"
If you want to copy and preserve both versions into the same folder, then at least one version will have to be renamed. You will have to decide what kind of naming scheme you want to use before you can begin coming up with a solution.

Resources