Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-03-12
11
Medium Priority
?
552 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 35

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 1000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 35

Accepted Solution

by:
Dan Craciun earned 1000 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 35

Expert Comment

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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …
Suggested Courses

963 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