Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8566423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:39:05+00:00 2026-06-11T17:39:05+00:00

I am writing a short batch script which attempts to take the first line

  • 0

I am writing a short batch script which attempts to take the first line of text file and remove the trailing backslash if present. This will then repeat for the remaining lines of the input file. However, when I run the script the line does not remove the backslash. My guess is this is a simple fix, but I have tried several troubleshooting methods with no luck. The code is posted below for reference. Thanks!

@echo on
setLocal EnableDelayedExpansion

::set firstline to firstline of test.txt
set /p firstline=<test.txt

::Remove trailing slash if present
IF !firstline:~-1!==\ SET firstline=!firstline:~0,-1!

::Output firstline (without \ to new txt file)
echo !firstline!>test2.txt

endlocal

test.txt file:

C:\Desktop\example\path\ 

C:\Desktop\example\path\2\

C:\Desktop\example\path\test\  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T17:39:06+00:00Added an answer on June 11, 2026 at 5:39 pm

    You are correct that it’s a simple problem. In fact it is solved with the introduction of 4 characters, all the same one.

    Exposing a backslash in batch is a bad idea, it’s likely to confuse the interpreter into thinking that it’s dealing with an actual file or path. You should surround it with double-quotes. This is true with other characters, including !%&*?/\<>,. mostly special characters that deal with paths and files. There may be more, and some are only occasionally a problem. Even 1 and 2 can cause trouble when next to a redirection character.

    Just to let you know, neither setlocal or the delayed expansion notation ! are necessary here, although there is no harm in it. (Except that the delayed notation makes debugging harder as it did in this case, by showing, when ECHO is On, the variable name rather than it’s contents.)

    Also, when trimming the last character of a string you don’t need 0 in :~0,-1, using :~,-1 is sufficient. Though there is no harm in doing it either way.

    And lastly, the echo on is unnecessary, as echo on is the default. Echo On is more for just displaying small parts of the execution of a long file. Though, once again, there is no harm in it.

    @echo on
    setLocal EnableDelayedExpansion
    
    ::set firstline to firstline of test.txt
    set /p firstline=<test.txt
    
    ::Remove trailing slash if present
    ::Your error is below this
    IF "!firstline:~-1!"=="\" SET firstline=!firstline:~,-1!
    
    ::Output firstline (without \ to new txt file)
    echo !firstline!>test2.txt
    
    endlocal
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm interested in writing a short python script which uploads a short binary file
I am writing a short batch file to prepare a control library DLL with
I am writing a short script to sanitise folder and file names for upload
I'm writing a script (show_volume.sh) which may be called several times in short intervals.
I am writing a short python script to read line from stdin, but when
I am writing a short python script for fun to check a few pages
i am writing a short bat file that contours a xyz file with GMT
I'm writing a short text-adventure game and that game is ready, but now I
I'm writing a short to a file, using the following code in Java :
I am writing a short and simple profiler (in C), which is intended to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.