Script Cannot Find File

Script cannot find file

$0

In order to change the current working directory to the script's directory, put the following command right after the shebang line:

cd "$(dirname "$0")"

The $0 variable expands to the script name (path to the script), and dirname returns path to the script's directory.

Detecting the current working directory

You can use pwd command to get the current working directory. If you are actually running Bash (I'm not sure, since the shebang in your code points to /bin/sh), you can use the built-in $PWD variable:

PWD
The current working directory as set by the cd builtin.

Storing the script's path into variable

Alternatively, save the directory path into a variable, and use it in the script, e.g.:

dir="$(cd $(dirname "$0"); pwd)"
chmod 770 "$dir/somefile"

Double quotes

Note the use of double quotes. Double quotes prevent reinterpretation of special characters. It is also the way to pass strings containing spaces as a single word:

dir="some directory name"
cd "$dir"

Without double quotes the words are interpreted as separate arguments.

Windows batch script The system cannot find the file specified. with exclamation in filenames?

I don't see that you are using somewhere delayed expansion. So, either disable it with setlocal DisableDelayedExpansion in the start of your batch file or just remove it by removing line setlocal EnableDelayedExpansion.

However, if you want to keep it, do:

@echo off

SetLocal EnableDelayedExpansion

rem Code above (^^) if exists.

Setlocal DisableDelayedExpansion

set "folder=%~1"
set "count=0"

for /R "%folder%" %%G in (*) do (
set "fullpath=%%~fG"
set "fileExtension=%%~xG"
call :processFile
)
goto end

:processFile
echo "fullpath = %fullpath%"
echo "fileExtension = %fileExtension%"

ren "%fullpath%" "temporary_filename_500%fileExtension%"

set /a "count+=1"
echo/
goto :eof

:end
echo "%count% files processed."
pause
setlocal EnableDelayedExpansion

rem Your code below with active delayed expansion:

Note that:

  • You should always quote the variable name and the value in the set command like set "var=value" and in set /a like set /a "var+=1", etc.; see set /? for more information.
  • To find the full path of a file/folder for sure in a for loop, use the f modifier, like %%~fG.
  • Mentioned by Mofi here: don't use echo.; use echo/ for better practice.

See also the Phase 5 (Delayed Expansion) of this answer about how batch files are interpreted.

Cannot find JavaScript script file from HTML file script

Create static folder in your root directory add this line to settings.py

STATIC_URL = '/static/'

TCL/TK Wish can't find file in the same directory

Most likely your Tcl code is not being executed in the same directory as where the DBW.ini file is. The Tcl command pwd will return the directory where the code is executing. If this is not where the ini file is, a simple fix would be to specify the whole path to the file when you try to open it, something like:

[::ini::open C:/some/where/DBW.ini]


Related Topics



Leave a reply



Submit