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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:01:36+00:00 2026-05-21T05:01:36+00:00

I have a number of files with the same naming scheme. As a sample,

  • 0

I have a number of files with the same naming scheme. As a sample, four files are called “num_001_001.txt”, “num_002_001.txt”, “num_002_002.txt”, “num_002_003.txt”

The first set of numbers represents which “package” it’s from, and the second set of numbers is simply used to distinguish them from one another. So in this example we have one file in package 001, and three files in package 002.

I am writing a windows vista batch command to take all of the files and move them into their own directories, where each directory represents a different package. So I want to move all the files for package 001 into directory “001” and all for 002 into directory “002”

I have successfully written a script that will iterate over all of the txt files and echo them. I have also written a scrip that will move one file into another location, as well as creating the directory if it doesn’t exist.

Now I figure that I will need to use substrings, so I used the %var:~start,end% syntax to get them. As a test, I wrote this to verify that I can actually extract the substring and create a directory conditionally

@echo off
set temp=num_001_001.txt
NOT IF exist %temp:~0,7%\
  mkdir %temp:~0,7%

And it works. Great.
So then I added the for loop to it.

@echo off
FOR /R %%X IN (*.txt) DO (
  set temp=%%~nX
  echo directory %temp:~0,7%
)

But this is my output:

directory num_002
directory num_002
directory num_002
directory num_002

What’s wrong? Does vista not support re-assigning variables in each iteration?
The four files are in my directory, and one of them should create num_001. I put in different files with 003 004 005 and all of it was the last package’s name. I’m guessing something’s wrong with how I’m setting things.

I have different workarounds to get the job done but I’m baffled why such a simple concept wouldn’t work.

  • 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-21T05:01:36+00:00Added an answer on May 21, 2026 at 5:01 am

    Your problem is that the variable get replaced when the batch processor reads the for command, before it is executed.

    Try this:

    SET temp=Hello, world!
    CALL yourbatchfile.bat
    

    And you’ll see Hello printed 5 times.

    The solution is delayed expansion; you need to first enable it, and then use !temp! instead of %temp%:

    @echo off
    SETLOCAL ENABLEDELAYEDEXPANSION
    FOR /R %%X IN (*.txt) DO (
      set temp=%%~nX
      echo directory !temp:~0,7!
    )
    

    See here for more details.

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

Sidebar

Related Questions

i have a number of files that i want to have the same base
I have a number of files that I checked into SVN without having set
I have a number of files I am loading into a slideshow, I have
i have a number of jsp files under web-inf folder. Inside my web.xml i
I have a large number of files in a .tar.gz archive. Checking the file
I have huge number of Word files I need to merge (join) into one
I have a large number of files in a ClearCase directory structure, and I
I have an arbitrary number of files that I need to load in an
I have a number of .css files spread across some directories. I need to
I have a number of generated .sql files that I want to run in

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.