Click here to Skip to main content
15,877,915 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi there,

I have the following line of Code in a Batch File :-

BAT
set "SourceFolder=C:\Users\eddyw\Desktop\PAC_PNL Misc\PAC-MISC\pacbob1940"


for %%a in ("%SourceFolder%\*.PAC") do PAC-MISC.exe -d Test_Files "%%a"


The Batch File runs a C Language Program, i.e. PAC-MISC.exe, -d is the parameter which saves to the chosen Folder, created if it doesn't exist, i.e. in this case to a folder named Test_Files. Each of the Files are processed, in the SourceFolder, but no bitmap Files are outputted and saved to the chosen Folder.

When I run PAC-MISC.exe from the command prompt for an individual Files in that folder the Program works, i.e. if I type for example :-

pac-misc -d Test_Files BFDASH.PAC that will output .bmp i.e, Bitmap Files to the Test_Files Folder.

Could anybody on this forum, suggest how I could modify my Batch File, to obtain outputted Files, in the chosen Folder ? Some of those .PAC Files in the SourceFolder, won't produce bitmap Files, but many will. I can't work out where I am going wrong here.

Any help and info would be much appreciated.

Regards

Eddie Winch

What I have tried:

The Code I have typed above in C Language, works for individual Files in Command Prompt.
Posted
Updated 18-Feb-23 13:37pm
Comments
Rick York 11-Feb-23 23:13pm    
This might be useful : https://www.microsoft.com/en-us/download/details.aspx?id=56846. It is a reference of windows commands.
Eddie Winch 19-Feb-23 16:31pm    
Many thanks for that link Rick, I will take a look at it.

@echo off

cd /d "C:\Users\eddyw\Desktop\PAC_PNL Misc\PAC-MISC"
pushd "C:\Users\eddyw\Desktop\PAC_PNL Misc\PAC-MISC\Test_Files"

for %%i in (*.PAC)do PAC-MISC.exe -d . "%%~nxi"

popd
I have a feeling that the program already handles its argument passing the value path\file by adding the double quotes, so that would explain why it worked in
...)do PAC-MISC.exe -d Test_Files %%a
Note that you can also pass the Name.eXtension of your file in the loop in addition to other options, like the full path, etc...
%%~Drive:\Path\Name.eXtension of %%i == > %%~dpnxi
In your case I would test further by removing the quotes if existing with %%~ and this will ensure that an output where "Value/Variable in loop" is just: value/variable in loop
%%~i :: removes quotes if existing in %%i value ::
You can define your variable with or without the use of quotation marks, this for your path or any other value, the point is to observe if at the end you have any other non-visible characters, such as space, tab... or if there is any other command on the same line side by side as:
set var=value & dir...
set var=value && dir...
set var=value || say...
which is different from something written intentionally as in:
set "var=value" & dir...
set "var=" || dir...
So, as a good practice, and for better visualization, using quotes in set "_variable=value" is regardless of whether you need it or not, but keeping in mind the use of quotes when necessary in the "variable" as needed when using it.

Finally, I would observe how the treatment of the command for the destination folder is received/interpreted, avoiding passing the full path or just the current name for the folder, or even the relative path, as in the situations below:
PAC-MISC.exe -d Test_Files %%a
PAC-MISC.exe -d "Test_Files" %%a
PAC-MISC.exe -d .\Test_Files %%a
PAC-MISC.exe -d "C:\Users\eddyw\Desktop\PAC_PNL Misc\PAC-MISC\Test_Files" %%a
If your command is running in the current folder and will also save there:
PAC-MISC.exe -d . %%a
PAC-MISC.exe -d %CD% %%a
PAC-MISC.exe -d "%CD%" %%a
If your command is running in another current folder and but it will save a folder above:
PAC-MISC.exe -d "..\Test_Files" %%a
What I propose is testing and observation of behavior results, considering that you don't have access to details about the code and/or how the arguments must be passed or how these arguments are interpreted and treated internally by the program. After that, actions to identify how to deal with these results in loop is easier to identify and resolve by itself.

Some references for querying the commands used:
Set - Environment Variable[^]
CD Change Directory[^]
Pushd - Popd change directory/folder[^]
Parameters / Arguments:
- Refer: %%~i Expand %%i removing any surrounding quotes"
[^]
 
Share this answer
 
v2
Comments
Eddie Winch 19-Feb-23 18:43pm    
Hi l o o l, Many thanks for this solution, I will have a good look at it tomorrow. Your
information is very much appreciated.
First, the SET statement does not need the quotes around it at all.

Next, the problem is going to be with the command you're running as part of the FOR statement, or the batch files ability to find the command.

Is the batch file in the same folder as PAC-MISC.EXE? If not, is the PAC-MISC.EXE file found in a folder in the PATH? If not, it's not going to work and generate an error that says something like: "'PAC-MISC.EXE' is not recognized as an internal or external command, operable program or batch file." You'll have to change the command to supply the fully qualified path to the PAC-MISC.EXE file so the batch file can run from any folder.
 
Share this answer
 
Comments
Eddie Winch 10-Feb-23 18:06pm    
Hi Dave,

Many thanks for your reply, Yes the PAC-MISC.EXE File is in the same Folder as the batch File, but not in the Source Folder, are you saying it needs to be there aswell ? Also do you mean the full path, to the Output Folder needs to be typed ?
Dave Kreskowiak 10-Feb-23 19:26pm    
No, I'm not saying that. I am saying that if you don't specify the full path to the PAC-MISC executable, you will have to have the batch file in the same folder as that executable.

Since you have both files in the same folder, there's nothing there that needs to change.

Now the problem is possibly the command line parameters you're providing to PAC-MISC.EXE in the batch file.

What is the full path to the folder that contains your batch file and the PAC-MISC.EXE file, and what is the full path to the Test_Files folder?
Eddie Winch 10-Feb-23 19:50pm    
Hi Dave,

I see what you mean, the full path to the Folder that contains the batch File is :-


C:\Users\eddyw\Desktop\PAC_PNL Misc\PAC-MISC\PAC-MISC.EXE

And to the Test_Files Folder is :-

C:\Users\eddyw\Desktop\PAC_PNL Misc\PAC-MISC\Test_Files
Dave Kreskowiak 10-Feb-23 19:52pm    
The batch file should work. If it still doesn't generate the images, then it's going to be a problem with the parameters passed to PAC-MISC.EXE in the FOR statement.
Eddie Winch 10-Feb-23 20:20pm    
And when I remove the "" around the %%a and run the batch file again, it doesn't process the .PAC Files.
You have a space character in the directory path, which will cause the path passed in the for statement to be interpreted as "C:\Users\eddyw\Desktop\PAC_PNL". Try changing the set statement to the following, removing the double quote in front of SourceFolder and inserting it in front of the actual directory path name.
Shell
set SourceFolder="C:\Users\eddyw\Desktop\PAC_PNL Misc\PAC-MISC\pacbob1940"
 
Share this answer
 
Comments
Eddie Winch 11-Feb-23 6:33am    
Hi Richard,

Many thanks for your reply, unfortunately what you suggested I do, didn't work, doesn't even process the Files, I also tried changing the set line of Code, after changing the Folder to PAC_PNL instead of PAC_PNL Misc to :-

set "SourceFolder=C:\Users\eddyw\Desktop\PAC_PNL\PAC-MISC\pacbob1940" instead but still no output files to the Test_Files Folder.
Richard MacCutchan 11-Feb-23 6:40am    
That makes no sense, and suggests that you trying to guess the path of the folder you are trying to refer to. So go back to the system and check the exact path name.
Eddie Winch 11-Feb-23 7:26am    
Hi Richard,

All the Paths and Folder names appear to be correct. I derived the batch File I made, from the following Forum thread :-

https://stackoverflow.com/questions/38957871/how-to-run-an-executable-on-all-files-in-a-directory-matching-a-file-extension-w
Richard MacCutchan 11-Feb-23 7:56am    
I have tried this on my system and as far as I can see you need to remove the quotes round %SourceFolder%in the second line, so it should be:
set SourceFolder="C:\Users\eddyw\Desktop\PAC_PNL Misc\PAC-MISC\pacbob1940"

for %%a in (%SourceFolder%\*.PAC) do PAC-MISC.exe -d Test_Files "%%a"
Eddie Winch 11-Feb-23 8:47am    
Hi Richard, That runs and processes the Files this time, but still no output .bmp Files in the Test_Files Folder. I really feel this should work, I could search for more threads, and alter a few things, if I find anything useful. Also I removed the "" around the last %%a, and it processes the Files, like with "%%a".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900