“访问被拒绝”我刚刚制作的文本文件出现错误消息?

发布于 2024-10-05 22:06:42 字数 5047 浏览 4 评论 0原文

大约 3 天前,我问了一个问题,可以在这里找到:

如何使用批处理文件替换文本文件中第二行的字符串?

我正在将文本文件中的字母转换为他们各自的号码。我收到诸如“访问被拒绝”和“无法找到此文件”之类的错误消息 - 但给我所有这些错误的同一个批处理文件也是最初生成这些文本文件的批处理文件!所以它应该与批处理文件本身位于同一目录中(除非另有指定),对吗?我什至去那个文件夹检查了一下,它们就在那里。

我确实添加了一个小脚本来隐藏文件创建后的内容,这样它就不会看起来那么混乱。我通过以下方法做到了这一点:使用

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

此命令隐藏文件是否会使正在搜索该文件/调用该文件的批处理程序看不到该文件?

以下是文件“stringparsing.bat”的链接: http://uploading.com/ files/a1m1d2f4/stringparsing.bat/

如果您能帮助我让这个程序在没有任何错误的情况下执行其任务,我将不胜感激!

这是完整的“stringparsing.bat”文件:

@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


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

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


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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

西瑶 2024-10-12 22:06:42

你有一些问题。首先,访问被拒绝的问题是由于您重定向到隐藏文件造成的。

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

请注意,第一次运行该脚本时,它将起作用,因为 Language_File.txt 不存在,因此不会被隐藏。第二次运行它时,您将被拒绝访问。我不知道为什么 Windows 不允许你这样做。您可以通过多种方式解决这个问题。

1. 将文件保存到用户的临时目录中。通过这种方法,您的目录不会变得混乱。

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

2. 将文件保存到您拥有的子目录中,以免使脚本目录混乱。

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

3. 在使用文件之前取消隐藏该文件。由于该文件在您第一次运行脚本时可能不存在,因此也许您应该仅attrib -h(如果它存在)。

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

4. 根本不要使用Language_File.txt!我不明白你为什么需要它。只需使用变量来保存语言文件的名称。事实上,您已经在 %name% 中拥有了该名称,对吧?

其次,您应该检查变量的值以了解它们真正包含的内容。当您将 Language_File.txt 的内容加载到变量中时,它会加载所有内容。这包括隐藏的换行符 \r\n,尽管脚本似乎将它们作为空格带入变量中。请参阅:

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  -

如果您回显由连字符包围的 %name%,您可以看到它后面有 2 个空格(大概是换行符)。为了解决这个问题,可以使用set来修剪尾随字符。

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-

在第二个示例中,`%name% 没有隐藏字符。

最后,您只需使用 ! 即可访问您在 for 循环中设置的变量。因此,所有对 !name! 的引用都应该是 %name% 。这可能是您的“找不到文件”错误。

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.

幻梦 2024-10-12 22:06:42

这是“stringparsing.bat”文件:

@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


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

我终于学会了如何格式化代码片段。
(如果需要,这里有另一个副本的链接。)

如何使用批处理文件替换文本文件中第二行的字符串?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文