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

  • Home
  • SEARCH
  • 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 8907345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:53:31+00:00 2026-06-15T02:53:31+00:00

I have 2 directories: dirA and dirB. We will only take dirB at the

  • 0

I have 2 directories: dirA and dirB. We will only take dirB at the moment.
dirB contains many files, named of course with different names/extension.

I need to extract the first 6 letters from each of them, after this I can continue with the 2nd part of the program, for now I only need to extract the first 6 letters from each file and write them on a txt files.

Here’s the code I wrote, the FOR is the only one that doesn’t work well, or better, instead of having the same variable: STR named file1 at loop1, file2 at loop2 etc.

I have STR = file1file2fileN.
var is file1f (6 letters already).

@ECHO off
CLS

SET dirA=C:\dirA
SET dirB=C:\dirB
SET lista=C:\lista.txt
IF NOT EXIST %dirB% GOTO DNF
IF NOT EXIST %dirA% GOTO DNF

setLocal EnableDelayedExpansion
CD %dirB%
DIR /B * > %lista%

:MAIN
CLS
SET /P S=Last Filename: || Set S=null

:LOOP 
IF /I "%S%"=="null" GOTO MAIN 
FOR /F "tokens=*" %%I IN (%lista%) DO SET str=!str!%%I
SET var=!str:~0,6!
echo !str! > str.txt
echo !var! >> file.txt
SET !str!%%I
IF /I %S%==!var! GOTO:EOF
IF /I %S% NEQ !var! GOTO SNF
GOTO LOOP

:DNF
ECHO.
ECHO %dirA% - %dirB% not found
GOTO:EOF

:SNF
ECHO.
ECHO %S%  Not found
TIMEOUT /T 2
GOTO MAIN

GOTO:EOF

endLocal
end
  • 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-15T02:53:32+00:00Added an answer on June 15, 2026 at 2:53 am

    Excuse me. I think I don’t understand what your code tries to achieve. However, this Batch segment “extract the first 6 letters from each file and write them on a txt files”:

    (for %%I in (*.*) do (
       set str=%%I
       echo !str:~0,6!
    )) > file.txt
    

    It is more efficient to enclose the whole FOR command in parentheses and use a single > redirect operation, than use an >> append operation for each file name.

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

Sidebar

Related Questions

I have many directories with files in them. I want to create a comma
Hi I have many directories which have blank space in their names.I have some
I will have 2 directories, folder1 with a set files list, and folder2 with
I have two directories ( dirA and dirB ), and i want to copy
I have massive directories, and I would like to read all the files as
Looking in my /usr/local/lib/python.../dist-package directory, I have .egg directories and .egg files. Why does
I have a few directories and sub-directories containing files with no file extension. I
I have a file ( directories.txt ) with directory names, each on a single
Imagine I have a directory structure like so: parentDir\dirA\foo\ parentDir\dirB\foo\ parentDir\dirC\ parentDir\dirD\bar\foo\ parentDir\dirE\foo\ parentDir\dirF\
I need display names directories in one directory. In dir I have directories and

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.