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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:33:16+00:00 2026-06-12T19:33:16+00:00

I am trying to renaming every file’s empty space with an underscore: DirectoryInfo dir

  • 0

I am trying to renaming every file’s empty space with an underscore:

DirectoryInfo dir = new DirectoryInfo(@"Q:\Audio");
var files = (from f in dir.GetFiles() select f.FullName);
files.ToList().ConvertAll( s => s.Replace( " ", "_") );

But it’s not working, I tried to use a foreach loop and it complains “Cannot assign to ‘element’ because it is a ‘foreach iteration variable'”

How do I approach this?

  • 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-12T19:33:17+00:00Added an answer on June 12, 2026 at 7:33 pm

    You can use one of code blocks below:

    DirectoryInfo dir = new DirectoryInfo(@"C:\");
    var files = (from f in dir.GetFiles() select f.FullName.Replace(" ", "_"));
    

    or:

    DirectoryInfo dir = new DirectoryInfo(@"C:\");
    var files = (from f in dir.GetFiles() select f.FullName);
    var fileNames = (from fn in files select fn.Replace(" ", "_"));
    

    or:

    DirectoryInfo dir = new DirectoryInfo(@"C:\");
    var files = (from f in dir.GetFiles() select f.FullName);
    var fileNames = files.Select(s => s.Replace(" ", "_"));
    

    or (Bad Idea):

    DirectoryInfo dir = new DirectoryInfo(@"C:\");
    var files = (from f in dir.GetFiles() select f.FullName);
    var fileNames = files.ToList().ConvertAll(s => s.Replace(" ", "_"));
    

    EDIT 1:
    but i suggest to use select function, its better to not call .ToList() method for deferred objects like IQueryable.

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

Sidebar

Related Questions

Recursive renaming files using PS is trivial (variation on example from Mike Ormond's blog
I'm trying to create a NSTextField to allow batch renaming of some files. This
I am trying to extract a range of pages from a multipage pdf file
(on Mac) I'm trying to clone my project from my new computer. I first
I am trying to delete a line from a text file in java, I
I'm trying to convert the Screen Space Ambient Occlusion example from XNA 3.1 to
I'm writing a BAT file for renaming some files. My files are *.jpg where
I am trying to rename a file from a modal window. The modal window
I'm trying to build a little renaming program to help save me time in
EDIT: When renaming ProfileService.class to ProfileService2.class, it worked... Name clash somewhere? When trying 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.