Solved

How to not hard code a file path in a Batch File

Posted on 2014-03-12
11
525 Views
Last Modified: 2014-03-12
Is there a way to code so that when batch file is pasted into a directory it can find out which directory it is instead of hardcoding it like this?
set SourceFolder=C:\Temp\MyFiles
0
Comment
Question by:regsamp
  • 4
  • 4
  • 3
11 Comments
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39924847
If I remember correctly, %0 was the name of the batch, including the path.

HTH,
Dan
0
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 250 total points
ID: 39924856
Sure.  The batch filename itself is %0 and you can get different bits of that with %*dp0 where the * character is the sqiggly line 'tilde'.  the d means drive, p means path and you can also use n for name and x for extension.  to change the current dir to the path of the batch file use:

@echo off
pushd "%*dp0"
echo You are now in %cd%, do your stuff
popd

Sorry can't type tilde character through mobile site it is treated as some shortcut key.

Steve
0
 

Author Comment

by:regsamp
ID: 39924861
So say at the beginning of this script, If I replace the set SourceFolder= to %0 It can just be copied into any directory and it will know the Source?
@echo off
setlocal enabledelayedexpansion
set SourceFolder=F:\Storage\Projects\TESTTHREE
set SourceMask=00000-*.dwg
:Loop
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 43

Expert Comment

by:Steve Knight
ID: 39924863
BTW another good technique can be to allow a directory to be dragged onto the batch file for processing, or prompting for a folder if the bqtch is run without a dir.  can show you those too if wanted.

steve
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39924868
%*dp0 though yes, %0 will be the full filename of the batch file that was run,  above is just the drive and path, i.e. the folder that you want.
0
 

Author Comment

by:regsamp
ID: 39924869
So here is the exact coding. How could I make it so that the batch file can just be copied into a directory and then the user just clicks on it and it does the default path of where it is at?

@echo off
setlocal enabledelayedexpansion
set SourceFolder=C:\Temp\MyFiles
set SourceMask=00000-*.dwg
:Loop
echo Please enter the new prefix for all '%SourceMask%' files in %SourceFolder%.
set /p NewPrefix=Maximum of 10 characters, empty input cancels:
if "%NewPrefix%"=="" goto :eof
if not "%NewPrefix:~10,1%"=="" (
      echo Error: your input is longer than 10 characters!
      echo.
      goto Loop
)
for /f "delims=" %%a in ('dir /b "%SourceFolder%\%SourceMask%"') do (
      echo Processing %%a ...
      for /f "tokens=1* delims=-" %%n in ("%%a") do set NewName=%NewPrefix%-%%o
      echo ... new name: !NewName!
      ECHO ren "%SourceFolder%\%%a" "!NewName!"
)
0
 
LVL 34

Accepted Solution

by:
Dan Craciun earned 250 total points
ID: 39924870
You need
set SourceFolder=%~dp0
0
 

Author Comment

by:regsamp
ID: 39924873
Okay, thank you Dan and Dragon. Let me test it.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39924892
BTW I mean this technique to have the batch file prompt for a folder, or allow a folder to drag from explorer onto it: http://scripts.dragon-it.co.uk/links/batch-gui-folder-mk2

Steve
0
 

Author Comment

by:regsamp
ID: 39924955
That worked perfectly. Thank you. Steve and Dan.
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39924961
Glad I could help!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question