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 680311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:21:30+00:00 2026-05-14T01:21:30+00:00

I need a script that inserts a number at some point into a file

  • 0

I need a script that inserts a number at some point into a file path, like this:
from C:\WINDOWS\system32\ansi.sys
to C:\WINDOWS\system32\5.ansi.sys

I made a function using a label and it seems to work fine, until I invoke it from within a FOR loop.

This is the code I wrote:

@ECHO OFF

SET PATHTOPROCESS="C:\WINDOWS\system32\ansi.sys"

call :GetNewFilePath NEWPATH %PATHTOPROCESS% 7
echo New file name: %NEWPATH%

call :GetNewFilePath NEWPATH %PATHTOPROCESS% 5
echo New file name: %NEWPATH%

FOR /L %%i in (1,1,2) DO (
    call :GetNewFilePath NEWPATH %PATHTOPROCESS% %%i
    echo New file name in FOR loop: %NEWPATH%
)

goto :eof


:GetNewFilePath
SETLOCAL
SET PINDEX=%~3
SET PDIRECTORY=%~dp2
SET PFILE=%~nx2
SET LocalVar1=%PDIRECTORY%%PINDEX%.%PFILE%
ENDLOCAL & IF "%~1" NEQ "" SET %~1=%LocalVar1%
goto :eof

and this is the output I get:

New file name: C:\WINDOWS\system32\7.ansi.sys
New file name: C:\WINDOWS\system32\5.ansi.sys
New file name in FOR loop: C:\WINDOWS\system32\5.ansi.sys
New file name in FOR loop: C:\WINDOWS\system32\5.ansi.sys

and this is what I expect:

New file name: C:\WINDOWS\system32\7.ansi.sys
New file name: C:\WINDOWS\system32\5.ansi.sys
New file name in FOR loop: C:\WINDOWS\system32\1.ansi.sys
New file name in FOR loop: C:\WINDOWS\system32\2.ansi.sys

It seems that in the FOR loop, the variable is reverted back to the value it had before calling the function. Is there some special trick required for this when using FOR?

Thank you.

  • 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-05-14T01:21:30+00:00Added an answer on May 14, 2026 at 1:21 am

    The variables inside the for loop are expanded immediately when that block is parsed, not when it’s run. You’ll need to use delayed expansion:

    rem this should preferrably go at the very start of the batch file
    setlocal enabladelayedexpansion
    
    ...
    
    FOR /L %%i in (1,1,2) DO ( 
        call :GetNewFilePath NEWPATH %PATHTOPROCESS% %%i
        echo New file name in FOR loop: !NEWPATH!
    )
    

    Otherwise %NEWPATH% would be expanded to the value it had before the loop and inside the loop there wouldn’t be any variable left.

    You can easily verify that when removing the echo off at the start, by the way. The loop’s block would then show the expanded value before it even ran. See help set for a more in-depth discussion of delayed expansion.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 371k
  • Answers 371k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer That's because MyCacheManager isn't exactly like the EntLib one! And… May 14, 2026 at 7:08 pm
  • Editorial Team
    Editorial Team added an answer You handle the GridView.RowDataBound event. This explains things... http://www.aspnettutorials.com/tutorials/controls/dropdownlist-gridview-csharp.aspx May 14, 2026 at 7:08 pm
  • Editorial Team
    Editorial Team added an answer As stated in the urllib2 documentation: The urllib2 module has… May 14, 2026 at 7:07 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.