Solved

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

Posted on 2014-03-12
11
522 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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 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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

744 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