Solved

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

Posted on 2014-03-12
11
524 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
 
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
DevOps Toolchain Recommendations

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

 

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

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

914 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now