Solved

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

Posted on 2014-03-12
11
527 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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 strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

856 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