“access denied” error message for a text file i just made?

和自甴很熟 提交于 2019-12-24 17:42:26

问题


About 3 days ago I asked a question, which can be found here:

how to replace a string on the second line in a text file using a batch file?

I'm converting the letters in a text file into their respective numbers. I'm getting error messages such as "Access denied" and "Cannot locate this file" -- but the same batch file that's giving me all these errors is also the one that made these text files to begin with! So it should be in the same directory as the batch file itself (unless specified otherwise), right? I even went to that folder and checked, and they're there.

I did add a small script to hide the files after they were created so that it wouldn't look so cluttered up. I did this by using

attrib +h C:\script\%name%.txt

Would hiding a file with this command make it invisible to batch programs that are searching for it/call upon it?

Here's a link to the file, "stringparsing.bat": http://uploading.com/files/a1m1d2f4/stringparsing.bat/

If you could assist me in getting this program to carry out its task without any errors it would be greatly appreciated!

Here's the "stringparsing.bat" file in full:

@echo off
setlocal enabledelayedexpansion 
title BETA
cls
cd C:\script\st
echo.
echo.
echo.
echo      Setting Variables...
echo      Loading Language Database...

:: ###################################################################################
::    CALLING VARIABLE DATABASE CALLING VARIABLE DATABASE CALLING VARIABLE DATABASE
:: ###################################################################################




TIMEOUT /t 5 /nobreak > nul

goto MAIN

:MAIN
set foo=0
cls
echo.
echo.
echo.
echo.
echo            ===================================
echo             #################################        
echo             #######     Main Menu:    #######
echo             #################################
echo            ===================================
echo.
echo.
echo             1.) Create New Language File...
echo.
echo             2.) Load Existing Lanuage File...
echo.
echo             3.) Settings...

echo ---------------------------------------------------------

SET /p CHOICE= Select a Function:
IF %CHOICE%== 1 GOTO CREATE
IF %CHOICE%== 2 GOTO LOAD
IF %CHOICE%== 3 GOTO SETTINGS

GOTO MAIN

:CREATE
cls
title Step 1
echo.
echo.
echo.
echo         =================================================================================
echo.
set /p name=  please type a name for your new language file:
echo.
echo         =================================================================================
cls
echo. > %name%.txt
echo.
echo.
echo.
echo        ==============================================================
echo        ##############################################################
echo        #============================================================#
echo        #                                                            #
echo        # - After you hit enter you will be redirected               #
echo        #   to a Live Typer. so anything you type into               #
echo        #   it will be sent to %name%.txt.                           #
echo        #                                                            #
echo        #                                                            #
echo        # - Next, select load language File For Encoding!            #
echo        #                                                            #
echo        #============================================================#
echo        ##############################################################
echo        ==============================================================

set /p line1= :
echo %line1% >> %name%.txt 2> nul


echo %name% > Language_File.txt
attrib +h Language_File.txt
set /a foo+ =1
)
echo.
echo ==========================================================

goto LOAD



:LOAD
set /a foo+ =1
IF %foo%== 2 goto loadexternal
goto LOAD23

:loadexternal
echo.
echo language file is loading now!
set /p name=<Language_File.txt
timeout /t 4 /nobreak > nul
echo.
echo.
echo Language_File Loaded!
pause >nul
goto LOAD23

:LOAD23
cls
echo.
echo.
echo.
echo.
echo.
echo        Encoding Your Language File... Please Wait... 
echo.
echo.
echo.

for /f "delims=" %%i in (!name!.txt) do ( 
echo translating "%%i"
set var=%%i 
set var=!var:a=1 ! 
set var=!var:b=2 ! 
set var=!var:c=3 ! 
set var=!var:d=4 ! 
set var=!var:e=5 ! 
set var=!var:f=6 ! 
set var=!var:g=7 ! 
set var=!var:h=8 ! 
set var=!var:i=9 ! 
set var=!var:j=10 ! 
set var=!var:k=11 ! 
set var=!var:l=12 ! 
set var=!var:m=13 ! 
set var=!var:n=14 ! 
set var=!var:o=15 ! 
set var=!var:p=16 ! 
set var=!var:q=17 ! 
set var=!var:r=18 ! 
set var=!var:s=19 ! 
set var=!var:t=20 ! 
set var=!var:u=21 ! 
set var=!var:v=22 ! 
set var=!var:w=23 ! 
set var=!var:x=24 ! 
set var=!var:y=25 ! 
set var=!var:z=26 ! 

echo !var! 
) 

echo !var! > !name!.txt

pause >nul
TIMEOUT /t 5 /nobreak > nul

goto MAIN


:END
cls
title SHUTTING DOWN...
echo.
echo.
echo.
echo            Terminating service stream...
echo.
echo.
echo.
echo.
echo            Done! Thank you for using this program!
TIMEOUT /t 5 /nobreak > nul





::(%xx%) -1  I/O Stream= "SHELL.dll" 
:: IF EXIST [&1[Parser_2009]] exit

Exit


:: #####################################################################################

回答1:


You've got a few problems. First, the access denied problem is from you redirecting to a hidden file.

echo %name% > Language_File.txt
attrib +h Language_File.txt

Note that the first time you run the script, it will work because Language_File.txt won't exist and therefore won't be hidden. The second time you run it, you'll get access denied. I don't know why Windows doesn't let you do that. You can solve this problem in a couple ways.

1. Save your file to the user's temp directory. With this approach your directory won't get cluttered.

echo %name% > %TMP%\Language_File.txt

2. Save your file to a subdirectory that you own so that it doesn't clutter the script's directory.

if not exist workspace mkdir workspace
echo %name% > workspace\Language_File.txt

3. Unhide the file before you use it. Since the file may not exist the first time you run the script, perhaps you should only attrib -h if it exists.

if exist Language_File.txt attrib -h Language_File.txt
echo %name% > %TMP%\Language_File.txt
attrib +h Language_File.txt

4. Don't use Language_File.txt at all! I don't see why you need it. Just use variables to hold the name of the language file. In fact, you already have the name in %name%, right?

Second, you should check the value of your variables to see what they really hold. When you load the contents of Language_File.txt into your variable, it's loading all the contents. That includes the hidden newline characters \r\n, although the script seems to bring them into the variable as spaces. See:

c:\batch\t>echo language file is loading now!
language file is loading now
C:\batch\t>set /p name= <Language_File.txt
C:\batch\t>echo -%name%-
-langfile  -

If you echo %name% surrounded by hyphens, you can see that there are 2 spaces after it from (presumably) the newline characters. To solve this problem, you can use set to trim the trailing characters.

C:\batch\t>echo language file is loading now!
language file is loading now
C:\batch\t>set /p name= <Language_File.txt
C:\batch\t>set name=%name:~0,-2%
C:\batch\t>echo -%name%-
-langfile-

In the second example, `%name% doesn't have the hidden characters.

Finally, you only need to use ! to access variables that you set inside the for loop. So all references to !name! should be %name% instead. That's probably your "cannot find file" error.




回答2:


heres the "stringparsing.bat" file:

@echo off
setlocal enabledelayedexpansion 
title BETA
cls
cd C:\script\st
echo.
echo.
echo.
echo      Setting Variables...
echo      Loading Language Database...

:: ###################################################################################
::    CALLING VARIABLE DATABASE CALLING VARIABLE DATABASE CALLING VARIABLE DATABASE
:: ###################################################################################




TIMEOUT /t 5 /nobreak > nul

goto MAIN

:MAIN
set foo=0
cls
echo.
echo.
echo.
echo.
echo            ===================================
echo             #################################        
echo             #######     Main Menu:    #######
echo             #################################
echo            ===================================
echo.
echo.
echo             1.) Create New Language File...
echo.
echo             2.) Load Existing Lanuage File...
echo.
echo             3.) Settings...

echo ---------------------------------------------------------

SET /p CHOICE= Select a Function:
IF %CHOICE%== 1 GOTO CREATE
IF %CHOICE%== 2 GOTO LOAD
IF %CHOICE%== 3 GOTO SETTINGS

GOTO MAIN

:CREATE
cls
title Step 1
echo.
echo.
echo.
echo         =================================================================================
echo.
set /p name=  please type a name for your new language file:
echo.
echo         =================================================================================
cls
echo. > %name%.txt
echo.
echo.
echo.
echo        ==============================================================
echo        ##############################################################
echo        #============================================================#
echo        #                                                            #
echo        # - After you hit enter you will be redirected               #
echo        #   to a Live Typer. so anything you type into               #
echo        #   it will be sent to %name%.txt.                           #
echo        #                                                            #
echo        #                                                            #
echo        # - Next, select load language File For Encoding!            #
echo        #                                                            #
echo        #============================================================#
echo        ##############################################################
echo        ==============================================================

set /p line1= :
echo %line1% >> %name%.txt 2> nul


echo %name% > Language_File.txt
attrib +h Language_File.txt
set /a foo+ =1
)
echo.
echo ==========================================================

goto LOAD



:LOAD
set /a foo+ =1
IF %foo%== 2 goto loadexternal
goto LOAD23

:loadexternal
echo.
echo language file is loading now!
set /p name=<Language_File.txt
timeout /t 4 /nobreak > nul
echo.
echo.
echo Language_File Loaded!
pause >nul
goto LOAD23

:LOAD23
cls
echo.
echo.
echo.
echo.
echo.
echo        Encoding Your Language File... Please Wait... 
echo.
echo.
echo.

for /f "delims=" %%i in (!name!.txt) do ( 
echo translating "%%i"
set var=%%i 
set var=!var:a=1 ! 
set var=!var:b=2 ! 
set var=!var:c=3 ! 
set var=!var:d=4 ! 
set var=!var:e=5 ! 
set var=!var:f=6 ! 
set var=!var:g=7 ! 
set var=!var:h=8 ! 
set var=!var:i=9 ! 
set var=!var:j=10 ! 
set var=!var:k=11 ! 
set var=!var:l=12 ! 
set var=!var:m=13 ! 
set var=!var:n=14 ! 
set var=!var:o=15 ! 
set var=!var:p=16 ! 
set var=!var:q=17 ! 
set var=!var:r=18 ! 
set var=!var:s=19 ! 
set var=!var:t=20 ! 
set var=!var:u=21 ! 
set var=!var:v=22 ! 
set var=!var:w=23 ! 
set var=!var:x=24 ! 
set var=!var:y=25 ! 
set var=!var:z=26 ! 

echo !var! 
) 

echo !var! > !name!.txt

pause >nul
TIMEOUT /t 5 /nobreak > nul

goto MAIN


:END
cls
title SHUTTING DOWN...
echo.
echo.
echo.
echo            Terminating service stream...
echo.
echo.
echo.
echo.
echo            Done! Thank you for using this program!
TIMEOUT /t 5 /nobreak > nul





::(%xx%) -1  I/O Stream= "SHELL.dll" 
:: IF EXIST [&1[Parser_2009]] exit

Exit


:: #####################################################################################

I Finally learned how to format the code snippet. (heres a link to another copy of it if you need it.)

how to replace a string on the second line in a text file using a batch file?



来源:https://stackoverflow.com/questions/4339885/access-denied-error-message-for-a-text-file-i-just-made

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!